You are currently viewing How to create the Visual Studio Setup Project to install the driver’s file (.INF file)?

How to create the Visual Studio Setup Project to install the driver’s file (.INF file)?

How to create the Visual Studio Setup Project to install the driver’s file (.inf file)?

Driver Installation with Visual Studio Setup Project is too much easy. You need to implement custom action to install the drivers with the Visual Studio Setup Project.

Custom Action is the same as our general code. You can implement your custom logic during install, commit, rollback and uninstallation of the setup project.

For this tutorial, you required 2 projects within the same solution.
1) Window Form Application (Name: DriverInstallation in my case)
2) Visual Studio Setup Project ((Name: DriverInstallationSetup in my case)

Create Form Application and Setup Project
Our next step is to add Install Class into Window Form Application (DriverInstallation in my case).

Right Click on DriverInstallation Project -> Add -> Installer Class

Give Name as ProjectInstaller.cs ( You can change the name whatever you want) and click on Add button.

The default Installer Class file looks like this:

namespace DriverInstallation
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
        }
    }
}

Here we need to add other override methods to implement the custom logic.
ProjectInstaller.cs class look like this:

namespace DriverInstallation
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
        }
        public override void Install(IDictionary savedState)
        {
            base.Install(savedState);
            //Add custom code here
        }
        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
            //Add custom code here
        }

        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
            //Add custom code here
        }
      
        public override void Uninstall(IDictionary savedState)
        {

            base.Uninstall(savedState);
            //Add custom code here

        }
    }
}

Now we implement our driver installation code.
Add Driver Installation Method:

/// <summary>
        /// execute PnPutil.exe to install the driver INF file
        /// </summary>
        /// <param name="driverPath"></param>
        private static void driverInstall(string driverPath)
        {

            try
            {
                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";
                string a = @"" + driverPath + "";
                process.StartInfo.Arguments = "/c C:\\Windows\\System32\\PnPutil.exe -a \"" + driverPath + "\""; // where driverPath is path of .inf file
                process.Start();
                process.WaitForExit();
                process.Dispose();
                Console.WriteLine(@"Driver has been installed");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

Call the driverInstall Method from Install method.

try
            {
                
                string action = "";
                string assemblyPath = "";
                StringDictionary myStringDictionary = Context.Parameters;
                if (Context.Parameters.Count > 0)
                {
                    foreach (string myString in Context.Parameters.Keys)
                    {

                        if (myString.ToLower().Equals("action"))
                        {
                            action = Context.Parameters[myString];
                        }
                        if (myString.ToLower().Equals("assemblypath"))
                        {
                            assemblyPath = Context.Parameters[myString];
                        }

                    }
                    //remove last file name to get the assembly root path
                    assemblyPath = assemblyPath.Substring(0, assemblyPath.LastIndexOf("\\"));

                }

                string path_to_inf = assemblyPath + "\\driversFolder\\Driver.inf";
                driverInstall(path_to_inf);
                //}

            }
            catch { }

Note: Please create “driversFolder” folder within you release folder of the Window Form Application and “driversFolder” folder contain the Driver.inf file.

create driverfolder within release

Final ProjectInstaller.cs file looks like this:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace DriverInstallation
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
        }
        public override void Install(IDictionary savedState)
        {
            base.Install(savedState);
            //Add custom code here
            try
            {
                
                string action = "";
                string assemblyPath = "";
                StringDictionary myStringDictionary = Context.Parameters;
                if (Context.Parameters.Count > 0)
                {
                    foreach (string myString in Context.Parameters.Keys)
                    {

                        if (myString.ToLower().Equals("action"))
                        {
                            action = Context.Parameters[myString];
                        }
                        if (myString.ToLower().Equals("assemblypath"))
                        {
                            assemblyPath = Context.Parameters[myString];
                        }

                    }
                    //remove last file name to get the assembly root path
                    assemblyPath = assemblyPath.Substring(0, assemblyPath.LastIndexOf("\\"));

                }

                string path_to_inf = assemblyPath + "\\driversFolder\\Driver.inf";
                driverInstall(path_to_inf);
                //}

            }
            catch { }
        }
        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
            //Add custom code here
        }

        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
            //Add custom code here
        }
      
        public override void Uninstall(IDictionary savedState)
        {

            base.Uninstall(savedState);
            //Add custom code here

        }
        /// <summary>
        /// execute PnPutil.exe to install the driver INF file
        /// </summary>
        /// <param name="driverPath"></param>
        private static void driverInstall(string driverPath)
        {

            try
            {
                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";
                string a = @"" + driverPath + "";
                process.StartInfo.Arguments = "/c C:\\Windows\\System32\\PnPutil.exe -a \"" + driverPath + "\""; // where driverPath is path of .inf file
                process.Start();
                process.WaitForExit();
                process.Dispose();
                Console.WriteLine(@"Driver has been installed");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
   
}

Please rebuild your Window Form Application (As Release to generate the .exe file)

Next step is to create a setup project.
Right-click on the Setup Project (DriverInstallationSetup) -> View -> File System and Click on the Application Folder
Paste your entire Release folder in the Application Folder
Add Entire Release Folder in the Application Folder

Now Pass our application file (.exe) to the custom action.
– Right Click on Setup Project (DriverInstallationSetup) -> View -> Custom Actions
– Right Click on the Install Action -> Add Custom Action -> and Select DriverInstallation.exe from the Application folder.
– Perform the Same Action for all the action ( for Commit, Rollback and Uninstall)
Add Custom Action Into Setup Project

Now perform the last step rebuild the Setup Project (As of Release) to create the final Setup EXE along with the driver installation.

Install the Setup Project, Connect your device with the system and check the driver is installed or not.

Hope this will helps.