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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2032493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:51:54  来源:igfitidea点击:

install/uninstall an .inf driver programmatically using C# .net

c#.netinstalldriver

提问by Navaneeth

I am making an application using c#.net. It contains a filesystem minifilter driver also. I want to install and uninstall this driver programmatically using c# .net. Normally i can install this using the .INF file (by right click + press install).but I want to install this programmatically. There is an SDK function InstallHinfSection() for installing the .inf driver . I am looking for a .net equivalent for this function.

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

Regards

问候

Navaneeth

纳瓦尼特

采纳答案by Eilon

Try something like this:

尝试这样的事情:

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);

Then to call it:

然后调用它:

InstallHinfSection(IntPtr.Zero, IntPtr.Zero, "my path", 0);

I generated most of this signature using the P/Invoke Signature Generator.

我使用P/Invoke Signature Generator生成了大部分签名

The full details of this method and its parameters are on MSDN. According to MSDN the first parameter can be null, the second one mustbe null, and the last parameter mustbe 0. You only have to pass in the string parameter.

此方法及其参数的完整详细信息位于MSDN 上。根据MSDN,第一个参数可以为空,第二个必须为空,最后一个参数必须为0。你只需要传入字符串参数。

回答by Ravian

This simple code worked for me

这个简单的代码对我有用

    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");
    }