Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a folder with directories with mouse cursors, each directory has a .inf file that installs the cursor set that is in that directory and adds it to C:\Windows\Cursors, I want a file that when the user runs it as admin it runs all the .inf files so one doesn't have to go on them one by one please.

The file can be written by C# or another language, I will try to understand the other language's code.

The code below runs the files as installing them but asks for admin for each one separately.

What I have tried:

using System;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace RunAllCursorsInf
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            DirectoryInfo di = new DirectoryInfo(@"G:\OneDrive\all2\BestCursors\BestCursors");


            int i = 0;
            foreach (FileInfo fi in di.GetFiles("*.inf",SearchOption.AllDirectories))
            {
                i++;
                //InstallHinfSection(IntPtr.Zero, IntPtr.Zero, fi.FullName, 0);// I need to run each inf file here
                //Process.Start(fi.FullName);


                driverInstall(fi.FullName);
            }
            MessageBox.Show(i.ToString());
        }

        static void driverInstall(string s)
        {
            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 " + "\"" + s + "\""; // where driverPath is path of .inf file
            process.Start();
            process.WaitForExit();
            process.Dispose();
            //MessageBox.Show(@"Driver has been installed");
        }

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

    }
}


This code, with making the program run as admin, works but shows a small window of installation and sometimes opens the window of the clasic Mouse Settings in some inf files that request that.
Posted
Updated 3-May-23 6:08am
v5

 
Share this answer
 
Comments
John Smith 27 3-May-23 11:50am    
I know this one, it runs the inf files in notepad, not like right clicking and choosing: "Install".
OriginalGriff 3-May-23 12:03pm    
Then tell people what you actually want rather than saying "runs all the .inf files" - we can't tell what you need unless your type it ... :D

Have a look here: https://stackoverflow.com/questions/2032493/install-uninstall-an-inf-driver-programmatically-using-c-sharp-net[^]
John Smith 27 3-May-23 12:20pm    
I did it, see the updated question, but it opens a window each time when installing each cursor set, and for 279 sets it's bothering.
I'm not familiar with doing this, so I used this google search: c# How to make a file that installs all files with the extension .inf[^] and the first result appears to be what you are looking for: How to install *.inf files on C#??[^] it states:
You can using rundll32.exe to register inf files.

There are more details on that page.
 
Share this answer
 
Comments
John Smith 27 3-May-23 12:20pm    
I did it, see the updated question, but it opens a window each time when installing each cursor set, and for 279 sets it's bothering.
Graeme_Grant 3-May-23 12:25pm    
yes, that is because of how your are executing the process. You need to run in silent mode: Run a Process silently in background without any window[^]
John Smith 27 3-May-23 13:22pm    
I want to make sure that you don't think that I'm talking about the CMD window... it doesn't appear, what appears is the installation of the mouse cursors on the top left side of the screen with a gray background form.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900