C# 以管理员权限运行 cmd 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13807429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running cmd commands with Administrator rights
提问by user1547766
How can I run the command **cd..**behind the scenes of a Windows Form? (i.e. the user cannot see it)
如何**cd..**在 Windows 窗体的幕后运行命令?(即用户看不到它)
Thanks.
谢谢。
采纳答案by Chris Andrews
See System.Diagnostics.Process http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
请参阅 System.Diagnostics.Process http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
There is also this SO answer to this same exact question:https://stackoverflow.com/a/1469790/25882
对于同样的问题,也有这样的答案:https : //stackoverflow.com/a/1469790/25882
Example:
例子:
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();
回答by Salaros
The snippet is "invisible" to the user, and also it redirects the output so you can use it somehow (I suppose you need it).
该代码段对用户来说是“不可见的”,并且它还重定向输出,以便您可以以某种方式使用它(我想您需要它)。
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
}
}
回答by Picrofo Software
You may initialize a new System.Diagnostics.ProcessStartInfowhich has the information required for your process to start in addition to WindowStylethat indicates the window state to use when the process is started which can be Hidden, Maximized, Minimizedor Normal. In your case, we will set this as Hiddenso that the process which will be started won't be able to receive either input nor show the output from/to the user.
您可以初始化一个新的System.Diagnostics.ProcessStartInfo拥有你的过程除了启动所需的信息WindowStyle,表明该进程启动它可以使用的窗口状态Hidden,Maximized,Minimized或Normal。在您的情况下,我们会将其设置为Hidden这样,将启动的进程将无法接收输入或向用户显示输出。
Example
例子
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
Screenshot
截屏
The following screenshot represents the Task Manager showing one process which was started by our application. However, its Window is not visible.
以下屏幕截图表示任务管理器,显示了由我们的应用程序启动的一个进程。但是,它的窗口不可见。


Notice: The process started will not terminate even if you close your application.
注意:即使您关闭应用程序,启动的进程也不会终止。
Additionally, to run a Process as an Administrator you may set the Verbproperty of the process start info to runas
此外,要以管理员身份运行进程,您可以将Verb进程启动信息的属性设置为runas
Example
例子
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
Notice: If you have the User Account Control enabled, you may be asked to allow the process to start with elevated permissions first if the application that tried to call this process was not running with elevated permissions.
注意:如果您启用了用户帐户控制,如果尝试调用此进程的应用程序未以提升的权限运行,则可能会要求您首先允许以提升的权限启动该进程。
If you would like to skip the prompt, I think that you should allow your main application to start with elevated permissions. To do this, you'll need to open your application's manifest and make sure that the following line is added
如果您想跳过提示,我认为您应该允许您的主应用程序以提升的权限启动。为此,您需要打开应用程序的清单并确保添加了以下行
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
This will simply tell your application to start only with elevated permissions. So, when you call the process as an Administrator, there'll be no prompt as the process caller is being executed under an Administrator.
这只会告诉您的应用程序仅以提升的权限启动。因此,当您以管理员身份调用流程时,不会有任何提示,因为流程调用方正在以管理员身份执行。
Thanks,
I hope you find this helpful :)
谢谢,
我希望你觉得这有帮助:)

