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

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

谢谢。

请参阅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(); 

您可以初始化一个新的System.Diagnostics.ProcessStartInfo ,除了WindowStyle之外, WindowStyle具有启动进程所需的信息,该窗口指示启动进程时可以使用的窗口状态,可以是HiddenMaximizedMinimizedNormal 。 在您的情况下,我们将其设置为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 

截图

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

该进程正在运行而不显示其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 

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

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

  

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

谢谢,
我希望你觉得这有用 :)

该片段对用户来说是“不可见的”,并且它还会重定向输出,以便您可以某种方式使用它(我想你需要它)。

 string output = null; try { ProcessStartInfo ps = new ProcessStartInfo("cmd"); ps.Arguments = "/c cd.."; ps.UseShellExecute = false; // Redirects the standard output so it reads internally in out program ps.RedirectStandardOutput = true; // Starts the process using (Process p = Process.Start(ps)) { // Reads the output to a string output = p.StandardOutput.ReadToEnd(); // Waits for the process to exit must come *after* StandardOutput is "empty" // so that we don't deadlock because the intermediate kernel pipe is full. p.WaitForExit(); } } catch { // manage errors } finally { if(output != null) { // Process your output } }