使用C#.net以编程方式安装/卸载.inf驱动程序

Nav*_*eth 17 .net c# install driver

我正在使用c#.net创建一个应用程序.它还包含一个文件系统minifilter驱动程序.我想使用c#.net以编程方式安装和卸载此驱动程序.通常我可以使用.INF文件安装它(通过右键单击+按安装).但我想以编程方式安装它.有一个SDK函数InstallHinfSection()用于安装.inf驱动程序.我正在寻找这个功能的.net等价物.

问候

Navaneeth

Eil*_*lon 25

尝试这样的事情:

using System.Runtime.InteropServices;

[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);
Run Code Online (Sandbox Code Playgroud)

然后叫它:

InstallHinfSection(IntPtr.Zero, IntPtr.Zero, "my path", 0);
Run Code Online (Sandbox Code Playgroud)

我使用P/Invoke签名生成器生成了大部分签名.

该方法及其参数的完整细节在MSDN上.根据MSDN,第一个参数可以为null,第二个参数必须为null,最后一个参数必须为0.您只需要传入string参数.

  • 我应该澄清一下:.NET Framework不包含此API的托管代码版本..NET Framework包含很少的API,可以包含低级Win32 API,例如驱动程序安装API.通过声明P/Invoke方法,您可以直接从托管代码调用本机Win32 API. (5认同)
  • 在 Windows 10 上,您需要将“CharSet = CharSet.Unicode”添加到“DllImport()”调用中 (2认同)
  • @Macindows 将其放入 `DllImport()` 中,如下所示:`[DllImport(......., CharSet = CharSet.Unicode)]`。您可以查找“C# 属性语法”以查找有关此语法的更多信息。 (2认同)

小智 5

这个简单的代码对我有用

    private void driverInstall()
    {

        var process = new Process();
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.FileName = "cmd.exe";

        process.StartInfo.Arguments = "/c C:\\Windows\\System32\\InfDefaultInstall.exe " + driverPath; // where driverPath is path of .inf file
        process.Start();
        process.WaitForExit();
        process.Dispose();
        MessageBox.Show(@"Driver has been installed");
    }
Run Code Online (Sandbox Code Playgroud)