使用管理员权限运行cmd命令

use*_*766 14 c# winforms c#-4.0

如何**cd..**在Windows窗体的幕后运行命令?(即用户无法看到它)

谢谢.

Pic*_*are 17

您可以初始化一个新的System.Diagnostics.ProcessStartInfo拥有你的过程除了启动所需的信息WindowStyle,指示窗口状态时,该过程开始时它可以使用Hidden,Maximized,MinimizedNormal.在您的情况下,我们将其设置为Hidden使得将要启动的进程将无法接收输入或向用户显示输出.

System.Diagnostics.ProcessStartInfo myProcessInfo = new System.Diagnostics.ProcessStartInfo(); //Initializes a new ProcessStartInfo of name myProcessInfo
myProcessInfo.FileName = Environment.ExpandEnvironmentVariables("%SystemRoot%") + @"\System32\cmd.exe"; //Sets the FileName property of myProcessInfo to %SystemRoot%\System32\cmd.exe where %SystemRoot% is a system variable which is expanded using Environment.ExpandEnvironmentVariables
myProcessInfo.Arguments = "cd.."; //Sets the arguments to cd..
myProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Sets the WindowStyle of myProcessInfo which indicates the window state to use when the process is started to Hidden
System.Diagnostics.Process.Start(myProcessInfo); //Starts the process based on myProcessInfo
Run Code Online (Sandbox Code Playgroud)

截图

以下屏幕截图表示任务管理器显示由我们的应用程序启动的一个进程.但是,它的窗口不可见.

该进程正在运行而未显示其Window

注意:即使您关闭应用程序,启动的过程也不会终止.

此外,要以管理员身份运行流程,您可以将Verb流程启动信息的属性设置为runas

System.Diagnostics.ProcessStartInfo myProcessInfo = new System.Diagnostics.ProcessStartInfo(); //Initializes a new ProcessStartInfo of name myProcessInfo
myProcessInfo.FileName = Environment.ExpandEnvironmentVariables("%SystemRoot%") + @"\System32\cmd.exe"; //Sets the FileName property of myProcessInfo to %SystemRoot%\System32\cmd.exe where %SystemRoot% is a system variable which is expanded using Environment.ExpandEnvironmentVariables
myProcessInfo.Arguments = "cd.."; //Sets the arguments to cd..
myProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Sets the WindowStyle of myProcessInfo which indicates the window state to use when the process is started to Hidden
myProcessInfo.Verb = "runas"; //The process should start with elevated permissions
System.Diagnostics.Process.Start(myProcessInfo); //Starts the process based on myProcessInfo
Run Code Online (Sandbox Code Playgroud)

注意:如果启用了"用户帐户控制",则可能会要求您在尝试调用此进程的应用程序未使用提升的权限运行时,首先允许该进程以提升的权限启动.

如果您想跳过提示,我认为您应该允许您的主应用程序以提升的权限开始.为此,您需要打开应用程序的清单并确保添加以下行

<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
Run Code Online (Sandbox Code Playgroud)

这只会告诉您的应用程序只能使用提升的权限启动.因此,当您以管理员身份调用该流程时,由于在管理员下执行流程调用程序,因此不会出现提示.

谢谢,
我希望你觉得这很有帮助:)

  • 不要忘记参数处的 `/C` (2认同)

Chr*_*ews 15

请参阅System.Diagnostics.Process http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

对于同样的问题,也有这样的答案: https ://stackoverflow.com/a/1469790/25882

例:

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 copy /b Image1.jpg + Archive.rar Image2.jpg";
startInfo.Verb = "runas";
process.StartInfo = startInfo;
process.Start();
Run Code Online (Sandbox Code Playgroud)

  • 还要确保没有将UseShellExecute设置为false (3认同)