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

时间:2012-12-10 19:03:39

标签: c# winforms c#-4.0

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

感谢。

3 个答案:

答案 0 :(得分:15)

您可以初始化一个新的System.Diagnostics.ProcessStartInfo,其中除WindowStyle之外还包含启动流程所需的信息,指示启动流程时可以使用的窗口状态{{1} },HiddenMaximizedMinimized。在您的情况下,我们将其设置为Normal,以便将要启动的进程无法接收输入或显示用户的输出。

示例

Hidden

<强>截图

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

The process is running without showing its Window

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

此外,要以管理员身份运行流程,您可以将流程开始信息的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 属性设置为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

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

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

答案 1 :(得分:14)

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

答案 2 :(得分:0)

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

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
}
}