简体   繁体   English

运行命令行(cmd),需要“安全消息”

[英]run command line (cmd) that require “Security Message”

I want to run the command line pnputil in c# program. 我想在c#程序中运行命令行pnputil The program needs to install USB driver. 该程序需要安装USB驱动程序。 I know how to run cmd in c# program, but I have a different problem: 我知道如何在c#程序中运行cmd,但我有一个不同的问题:

The driver that I want to install does't have permission of windows. 我想安装的驱动程序没有Windows权限。

If I Install the driver via the "device manager->update driver" and choose the path of the driver, I get "Security Message" from windows that "windows can't verify the publisher of this drive software" and let me choose if install the driver or not (of course, if I choose to install - the installing succeeds). 如果我通过“设备管理器 - >更新驱动程序”安装驱动程序并选择驱动程序的路径,我从Windows获取“安全消息”“窗口无法验证此驱动器软件的发布者”并让我选择是否是否安装驱动程序(当然,如果我选择安装 - 安装成功)。

If I run the command from the cmd pnputil -a <path_name_inf> I get this message too and I can install the driver. 如果我从cmd pnputil -a <path_name_inf>运行命令,我也收到此消息,我可以安装驱动程序。

But when I try to run the command via c# program - the program runs but the driver is not installed (I also don't get this message). 但是当我尝试通过c#程序运行命令时 - 程序运行但是没有安装驱动程序(我也没有收到此消息)。

my code in c#: 我在c#中的代码:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = @"/C Pnputil -a <path_name_inf>";
process.StartInfo = startInfo;
process.Start();

How can I do that? 我怎样才能做到这一点?

You could try to run the cmd using the runas verb: 您可以尝试使用runas动词运行cmd

startInfo.Verb = "runas";
startInfo.UseShellExecute = true;

This parameter causes a privileges elevation. 此参数会导致权限提升。 The same thing could be reached when you use "Run as administrator" in explorer. 在资源管理器中使用“以管理员身份运行”时,可以达到同样的效果。

Your questions is more towards installing unsigned drivers. 您的问题更多的是安装未签名的驱动程序。

Can you please try following steps: Open a command prompt as an admin and type: 您可以尝试以下步骤:以管理员身份打开命令提示符并键入:

bcdedit -set TESTSIGNING ON

Please refer MSDN link for more infromation on Test Signing. 有关测试签名的更多信息,请参阅MSDN链接 Making Test signing may put a watermark as TEST in desktop waterpaper 进行测试签名可能会在桌面水印中添加水印作为TEST

I know this is old, but I wanted to share my solution in case it can help someone else. 我知道这是旧的,但我想分享我的解决方案,以防它可以帮助别人。

My specific scenario is that I wanted to add an unsigned driver package to the driver store on Windows 7 Home Premium 64-bit. 我的具体方案是我想在Windows 7 Home Premium 64位上将未签名的驱动程序包添加到驱动程序存储区。

Like the OP, this worked as expected (meaning I received the security warning and the driver package was added to the driver store) if I executed pnputil -a <path_to_inf> using a command prompt using "Run as administrator". 像OP一样,如果我使用“以管理员身份运行”命令提示符执行pnputil -a <path_to_inf>预期工作(意味着我收到了安全警告并且驱动程序包已添加到驱动程序存储区)。

Windows无法验证此驱动程序软件的发布者

However, if I tried to call pnputil from within C#, I was never able to get the security warning and the driver package was not added to the driver store. 但是,如果我尝试从C#中调用pnputil,我就无法获得安全警告,并且驱动程序包未添加到驱动程序存储区。 I had tried using various options (ie Verb = "runas", UseShellExecute = true, etc) with the FileName set as "pnputil" or "cmd". 我曾尝试使用各种选项(即Verb =“runas”,UseShellExecute = true等),并将FileName设置为“pnputil”或“c​​md”。

Ultimately, what worked for me was to create a simple batch file that contained the following: 最终,对我有用的是创建一个包含以下内容的简单批处理文件:

%windir%\sysnative\pnputil /a <path_to_inf>

My C# app would then call this batch file as follows: 然后,我的C#应用​​程序将调用此批处理文件,如下所示:

Process proc = new Process();
proc.StartInfo.FileName = "<path_to_bat_file>";
proc.StartInfo.Verb = "runas";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.CreateNoWindow = true;
proc.Start();

The user would first be prompted because I asked to run with elevated permission followed by the expected security warning. 首先会提示用户,因为我要求以提升的权限运行,然后是预期的安全警告。 The driver package would then be added to the driver store as expected. 然后,驱动程序包将按预期添加到驱动程序存储区。

用户帐户控制

If this does not work as expected, you can add "pause" (without quotes) on a new line after the last command in the batch file and remove the WindowStyle and CreateNoWindow lines from the C# code to see what is happening on the command prompt. 如果这不能按预期工作,您可以在批处理文件中的最后一个命令之后的新行上添加“pause”(不带引号),并从C#代码中删除WindowStyle和CreateNoWindow行以查看命令提示符上发生的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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