我試圖以管理員身份運行cmd命令。但CMD窗口意外關閉。如果CMD窗口停留,我可以看到錯誤。我試圖使用process.WaitForExit();
C#以管理員身份運行CMD
我試圖以管理員身份運行代碼zipalign -v 4 your_project_name-unaligned.apk your_project_name.apk
。
這是我的代碼。
//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();
}
有沒有辦法讓CMD窗口保持運行/顯示?
這是最有可能造成所有這些以及與string.replace'「'和\ –