[ACCEPTED]-install/uninstall an .inf driver programmatically using C# .net-driver

Accepted answer
Score: 29

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 6 generated most of this signature using the 5 P/Invoke Signature Generator.

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

Score: 7

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

0

Score: 1

Same as @Ravians answer only I've added 5 an ExitCode to check if the user has pressed yes 4 or no to the dialog. 0 been that they pressed 3 yes and 1 for no.

It also fixes the issue 2 for the file path of the inf file when it 1 has spaces.

public static readonly string windowsPath = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
public static bool driversInstalledSuccessfully;
public static readonly string driverPath = @"C:/Path to/Driver.inf";

private static void DriverInstall(string driverFile)
{

    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 " + windowsPath + "\\System32\\InfDefaultInstall.exe " + "\"" + driverPath + "\""; // where driverPath is path of .inf file
    process.Start();
    process.WaitForExit();

    if (process.ExitCode == 0)
    {
        Debug.WriteLine("Successfully Installed");
        driversInstalledSuccessfully = true;
    }
    else
    {
        Debug.WriteLine("Big Problemo");
        driversInstalledSuccessfully = false;
    }
    process.Dispose();

} // End DriverInstall

Called via

DriverInstall(driverPath);
Score: 0

//public static readonly string windowsPath 5 = Environment.GetFolderPath(Environment.SpecialFolder.Windows); //public 4 static bool driversInstalledSuccessfully; //public 3 static readonly string driverPath = Environment.CurrentDirectory 2 + "Driver\64bit\u4.inf";

    private static void DriverInstall(string driverFile)
    {

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

        if (process.ExitCode == 0)
        {
            Debug.WriteLine("Successfully Installed");
            driversInstalledSuccessfully = true;
        }
        else
        {
            Debug.WriteLine("Big Problemo");
            driversInstalledSuccessfully = false;
        }
        process.Dispose();

    }

//it gave 1 error the parameter is incorrect

More Related questions