winforms C#安装Cursor安装程序(.inf)文件

r1zhe5dt  于 2023-06-24  发布在  C#
关注(0)|答案(1)|浏览(149)

我有一个游标安装程序文件(installer.inf)。是否可以使用C#安装游标?
我已经尝试了下面的代码,但它不工作,执行时什么也没有发生:

[DllImport("Setupapi.dll", EntryPoint = "InstallHinfSection", CallingConvention = CallingConvention.StdCall)]
public static extern void InstallHinfSection([In] IntPtr hwnd, [In] IntPtr ModuleHandle, [In, MarshalAs(UnmanagedType.LPWStr)] string CmdLineBuffer, int nCmdShow);

public static void InstallCursor(string installerFilePath)
{
    InstallHinfSection(IntPtr.Zero, IntPtr.Zero, installerFilePath, 0);
}

通过游标安装程序,我指的是在Windows上安装新游标时存在的以下文件:

在这里,installer.inf文件看起来像这样:

[Version]
signature="$CHICAGO$"

[DefaultInstall]
CopyFiles = Scheme.Cur, Scheme.Txt
AddReg    = Scheme.Reg

[DestinationDirs]
Scheme.Cur = 10,"%CUR_DIR%"
Scheme.Txt = 10,"%CUR_DIR%"

[Scheme.Reg]
HKCU,"Control Panel\Cursors\Schemes","%SCHEME_NAME%",,"%10%\%CUR_DIR%\%pointer%,%10%\%CUR_DIR%\%help%,%10%\%CUR_DIR%\%work%,%10%\%CUR_DIR%\%busy%,%10%\%CUR_DIR%\%Cross%,%10%\%CUR_DIR%\%Text%,%10%\%CUR_DIR%\%Hand%,%10%\%CUR_DIR%\%Unavailiable%,%10%\%CUR_DIR%\%Vert%,%10%\%CUR_DIR%\%Horz%,%10%\%CUR_DIR%\%Dgn1%,%10%\%CUR_DIR%\%Dgn2%,%10%\%CUR_DIR%\%move%,%10%\%CUR_DIR%\%alternate%,%10%\%CUR_DIR%\%link%"

[Scheme.Cur]
"Normal Select.cur"
"Help Select.ani"
"Work in Background.ani"
"Wait.ani"
"Precision Select.cur"
"Text Select.cur"
"Handwriting.cur"
"Unavailable.ani"
"Vertical Resize.ani"
"Horizontal Resize.ani"
"Diagonal Resize 1.ani"
"Diagonal Resize 2.ani"
"Move.ani"
"Alternate Select.cur"
"Link Select.cur"

[Strings]
CUR_DIR         = "Cursors\pkuwyc\GANT_Blue_Colorful_24x24"
SCHEME_NAME     = "Gant Blue"
pointer     = "Normal Select.cur"
help        = "Help Select.ani"
work        = "Work in Background.ani"
busy        = "Wait.ani"
cross       = "Precision Select.cur"
text        = "Text Select.cur"
hand        = "Handwriting.cur"
unavailiable    = "Unavailable.ani"
vert        = "Vertical Resize.ani"
horz        = "Horizontal Resize.ani"
dgn1        = "Diagonal Resize 1.ani"
dgn2        = "Diagonal Resize 2.ani"
move        = "Move.ani"
alternate   = "Alternate Select.cur"
link        = "Link Select.cur"

我不明白太多的驱动程序安装程序,但因为它是一个.inf文件,它应该像一个驱动程序安装程序的权利?或者是Windows再次做了一些特殊的事情。

envsm3lx

envsm3lx1#

以下代码成功安装了游标:

public void InstallCursor(string installerFilePath)
{
    string command = @"C:\WINDOWS\System32\rundll32.exe";
    string arguments = "setupapi,InstallHinfSection DefaultInstall 132 " + installerFilePath;

    ProcessStartInfo startInfo = new ProcessStartInfo
    {
        FileName = "cmd.exe",
        Arguments = $"/c \"{command} {arguments}\"",
        Verb = "runas",
        UseShellExecute = true,
        CreateNoWindow = true,
        WindowStyle = ProcessWindowStyle.Hidden
    };

    Process process = new Process
    {
        StartInfo = startInfo
    };

    process.Start();
    process.WaitForExit();
}

请注意,您打开的cmd或打开它的应用程序都需要以管理员权限运行才能安装.inf文件

相关问题