[英]C# Run CMD as Administrator
I'm trying to run cmd command as administrator. 我正在尝试以管理员身份运行cmd命令。 But the CMD window closes unexpectedly.
但是CMD窗口意外关闭。 If CMD window stays I can see the error.
如果CMD窗口停留,我可以看到错误。 I tried to use
process.WaitForExit();
我试图使用
process.WaitForExit();
I am trying to run the code zipalign -v 4 your_project_name-unaligned.apk your_project_name.apk
as administrator. 我正在尝试以管理员身份运行代码
zipalign -v 4 your_project_name-unaligned.apk your_project_name.apk
。
Here is my code. 这是我的代码。
//The command that we want to run
string subCommand = zipAlignPath + " -v 4 ";
//The arguments to the command that we want to run
string subCommandArgs = apkPath + " release_aligned.apk";
//I am wrapping everything in a CMD /K command so that I can see the output and so that it stays up after executing
//Note: arguments in the sub command need to have their backslashes escaped which is taken care of below
string subCommandFinal = @"cmd /K \""" + subCommand.Replace(@"\", @"\\") + " " + subCommandArgs.Replace(@"\", @"\\") + @"\""";
//Run the runas command directly
ProcessStartInfo procStartInfo = new ProcessStartInfo("runas.exe");
//Create our arguments
string finalArgs = @"/env /user:Administrator """ + subCommandFinal + @"""";
procStartInfo.Arguments = finalArgs;
//command contains the command to be executed in cmd
using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
proc.StartInfo = procStartInfo;
proc.Start();
}
Is there a way to keep the CMD window running/showing? 有没有办法使CMD窗口保持运行/显示?
You are starting a process from the runas.exe
executable file. 您正在从
runas.exe
可执行文件启动进程。 That's not how to elevate a process. 那不是提升流程的方法。
Instead you need to use shell execute to start your excutable, but use the runas
verb. 相反,您需要使用shell execute来启动您的可执行文件,但要使用
runas
动词。 Along these lines: 遵循以下原则:
ProcessStartInfo psi = new ProcessStartInfo(...); // your command here
psi.UseShellExecute = true;
psi.Verb = "runas";
Process.Start(psi);
Capture the output(s) from your process: 捕获过程中的输出:
proc.StartInfo = procStartInfo;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start()
// string output = proc.StandardOutput.ReadToEnd();
string error = proc.StandardError.ReadToEnd();
proc.WaitForExit();
Then do something with the output. 然后对输出执行一些操作。
Note: you shouldn't try to synchronously read both streams at the same time, as there's a deadlock issue. 注意:由于存在死锁问题,因此您不应尝试同时同步读取两个流。 You can either add asyncronous reading for one or both, or just switch back and forth until you're done troubleshooting.
您可以为其中之一或两者添加异步读取,或者只是来回切换直到完成故障排除。
The following method actually works... 以下方法实际上有效...
private void runCMDFile()
{
string path = @"C:\Users\username\Desktop\yourFile.cmd";
Process proc = new Process();
proc.StartInfo.FileName = path;
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.CreateNoWindow = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.Verb = "runas";
proc.Start();
proc.WaitForExit();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.