C# 如何使用参数在管理模式下启动命令提示符以安装inf文件
我知道这是一个老问题,但我的问题非常关键,我尝试了很多在So或谷歌中建议的方法,但没有得到任何帮助。我想在产品安装期间安装一个C# 如何使用参数在管理模式下启动命令提示符以安装inf文件,c#,.net,windows,cmd,installation,C#,.net,Windows,Cmd,Installation,我知道这是一个老问题,但我的问题非常关键,我尝试了很多在So或谷歌中建议的方法,但没有得到任何帮助。我想在产品安装期间安装一个inf文件。所以我必须使用命令行参数来完成。我在“C:\Program Files\Com\ProductName” 在这个位置我有inf和sys文件。现在我已经编写了一个C代码来安装驱动程序 class install { static void Main(string[] args) { string str = "RUNDLL32.EX
inf
文件。所以我必须使用命令行参数来完成。我在“C:\Program Files\Com\ProductName”
在这个位置我有inf
和sys
文件。现在我已经编写了一个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->run as administrator运行相同的命令,并转到inf
文件所在的路径,然后运行该命令,则驱动程序安装成功,但如果我运行,则使用该代码,我没有得到任何异常,但在C:\Windows\System32\Drivers
文件夹中找不到驱动程序
所以驱动程序没有安装
请任何人帮我找出我犯的错误
请您正在运行的应用程序应该被授予管理员权限,有几种方法可以授予访问权限 1-清单
3-Registry
您可以通过注册表添加授予管理员权限作为兼容性标志,为此,您应该添加一个名为应用程序完整路径的密钥,并将值~RUNASADMIN
添加到HKEY\U CURRENT\U USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
key如果您想使用管理权限运行控制台应用程序
,您应该首先添加清单[app.manifest]
文件到控制台应用程序,请按照给定的步骤操作
添加清单文件后,必须更改清单文件中的以下设置:
将此更改为
这个
请注意,用户在本地计算机上应具有管理员权限,有时在应用程序执行过程中会提示用户输入凭据。否这不是控制台应用程序,这是使用wix脚本进行安装的一部分。在CustomActions中,我编写了此方法来安装驱动程序
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />