繁体   English   中英

以管理员权限运行cmd命令

[英]Running cmd commands with Administrator rights

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

谢谢。

您可以初始化一个新的System.Diagnostics.ProcessStartInfo ,除了WindowStyle以外, 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

屏幕截图

以下屏幕快照代表任务管理器,显示了由我们的应用程序启动的一个过程。 但是,其窗口不可见。

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

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

此外,要以管理员身份运行流程,可以将流程启动信息的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

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

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

<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>

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

谢谢,
我希望这个对你有用 :)

请参阅System.Diagnostics.Process http://msdn.microsoft.com/zh-cn/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();

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

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

暂无
暂无

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

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