简体   繁体   中英

How to start a command prompt in admin mode with arguments to install inf file

I know its a old question but my problem is very critical I tried many ways suggested in So or in google but didn't get any help. I want to install an inf file during our product installation. So I have to do it using a command line argument. I unpack the file in "C:\\Program Files\\Com\\ProductName" In this location I have the inf and the sys file. Now I have written a C# code to install the driver.

class install
{
    static void Main(string[] args)
    {
       string str = "RUNDLL32.EXE SETUPAPI.DLL,InstallHinfSection DefaultInstall 132 .\infname.inf";
         commandtorun(str);
      }
      static void commandtorun(string commandexecuted)
      {
         string currentstatus;
         ProcessStartInfo startInfo = new ProcessStartInfo();
         Process myprocess = new Process();
         try
         {
            startInfo.FileName = "cmd"; //
            startInfo.RedirectStandardInput = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false; 
            startInfo.CreateNoWindow = true;
            startInfo.WorkingDirectory = @"C:\Program Files\Com\ProductName";
            startInfo.Verb = "runas";

            myprocess.StartInfo = startInfo; 
            myprocess.Start();

            System.IO.StreamReader SR;
            System.IO.StreamWriter SW;
            Thread.Sleep(200);
            SR = myprocess.StandardOutput;
            SW = myprocess.StandardInput;
            SW.WriteLine(commandexecuted); 
            SW.WriteLine("exit"); 
            Thread.Sleep(200);
            currentstatus = SR.ReadToEnd();
            SW.Close();
            SR.Close();
         }
         catch (Exception e)
         {

         }
}

Now the problem is if I run the same command from start menu cmd-> run as administrator and go to the path where is the inf file and then run the command, the driver installs successfully, but using the code if I run, I don't get any exception but I could not find out the driver in the C:\\Windows\\System32\\Drivers folders

So the driver dididn't get installed.

Anybody please help me to figure out whats the mistake I am doing.

Please

Your running application should be granted with administrator privilege,there are several methods to grant the access

1- Manifest

<?xml version="1.0" encoding="utf-8" ?> 
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" 
xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" 
xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication" />
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
        <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            <requestedExecutionLevel level="requireAdministrator" 
    uiAccess="false" />
        </requestedPrivileges>
    </security>
</trustInfo>

2-Self-Elevation

a complete guide with samples are provided by Microsoft, here is the link: http://support.microsoft.com/kb/981778

3-Registry

You can add grant administrator privilege as Compatibility flag through registry, for doing this you should add a key with a name as full path of your application and value ~RUNASADMIN to HKEY_CURRENT_USER\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers key

If you want to run your console application with Admin Privilege you should first add a manifest[app.manifest] file to your console application, follow the steps given here .

Once you add the manifest file you will have to change the following settings in the manifest file,

Change this to

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

This

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Please note that user should have admin privilege in the local machine also sometimes user will be prompted to enter credentials during the application execution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM