如何在管理模式下用参数启动命令提示符来安装inf文件

本文关键字:命令提示符 安装 inf 文件 启动 参数 管理模式 | 更新日期: 2023-09-27 18:19:41

我知道这是一个老问题,但我的问题非常关键。我尝试了So或谷歌中建议的许多方法,但没有得到任何帮助。我想在产品安装过程中安装一个inf文件。所以我必须使用命令行参数。我在"C:'Program Files'Com'ProductName"中解压缩文件在这个位置,我有infsys文件。现在我已经编写了一个C#代码来安装驱动程序。

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)
         {
         }
}

现在的问题是,如果我从开始菜单cmd->以管理员身份运行相同的命令,并转到inf文件所在的路径,然后运行该命令,驱动程序安装成功,但如果我运行该代码,我没有得到任何异常,但我在C:'Windows'System32'Drivers文件夹中找不到驱动程序

所以驱动程序没有安装。

任何人都请帮我弄清楚我犯了什么错。

如何在管理模式下用参数启动命令提示符来安装inf文件

您正在运行的应用程序应该被授予管理员权限,有几种方法可以授予访问

1-清单

<?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-自提升

微软提供了一份完整的指南和样本,下面是链接:http://support.microsoft.com/kb/981778

3-注册表

您可以通过注册表添加授予管理员权限作为兼容性标志,为此,您应该添加一个名称为应用程序完整路径、值为~RUNASADMIN的密钥到HKEY_CURRENT_USER'Software'Microsoft'Windows NT'CurrentVersion'AppCompatFlags'Layers密钥

如果您想使用Admin Privilege运行控制台应用程序,您应该首先向控制台应用程序添加一个manifest[app.manifest]文件,请按照此处给出的步骤操作。

添加清单文件后,您必须更改清单文件中的以下设置,

将其更改为

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

这个

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

请注意,用户应该在本地计算机中具有管理员权限,有时还会在应用程序执行过程中提示用户输入凭据。