C# CMD 输出问题
Issue with C# CMD output
我正在创建一个 C# 应用程序,它将 Windows 服务器版本从标准评估更改为标准。我正在尝试获取 CMD 命令的输出,但是当 DISM 命令完成时,它会询问您是否要重新启动计算机并且您需要输入“y”或“n”。我尝试通过在命令前传递“echo n |”并使用 process.StandardInput.Write 来实现它,但是 none 有效。该功能与其他不需要用户输入的命令完美配合。你知道我做错了什么吗?谢谢
public static string get_cmd_output(string cmd)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/C echo n | " + cmd;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
string q = "";
while (!process.HasExited)
q += process.StandardOutput.ReadToEnd();
return q;
}
get_cmd_output("DISM /Online /Set-Edition:ServerStandard /ProductKey:" + key + " /AcceptEula");
在docs for DISM中,您可以传递的全局参数之一是/NoRestart
:
/NoRestart
Suppresses reboot. If a reboot is not required, this command does
nothing. This option will keep the application from prompting for a
restart (or keep it from restarting automatically if the /Quiet option
is used).
所以如果你这样做应该会起作用:
get_cmd_output("DISM /Online /Set-Edition:ServerStandard /ProductKey:" + key + " /AcceptEula /NoRestart");
我正在创建一个 C# 应用程序,它将 Windows 服务器版本从标准评估更改为标准。我正在尝试获取 CMD 命令的输出,但是当 DISM 命令完成时,它会询问您是否要重新启动计算机并且您需要输入“y”或“n”。我尝试通过在命令前传递“echo n |”并使用 process.StandardInput.Write 来实现它,但是 none 有效。该功能与其他不需要用户输入的命令完美配合。你知道我做错了什么吗?谢谢
public static string get_cmd_output(string cmd)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/C echo n | " + cmd;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
string q = "";
while (!process.HasExited)
q += process.StandardOutput.ReadToEnd();
return q;
}
get_cmd_output("DISM /Online /Set-Edition:ServerStandard /ProductKey:" + key + " /AcceptEula");
在docs for DISM中,您可以传递的全局参数之一是/NoRestart
:
/NoRestart
Suppresses reboot. If a reboot is not required, this command does nothing. This option will keep the application from prompting for a restart (or keep it from restarting automatically if the /Quiet option is used).
所以如果你这样做应该会起作用:
get_cmd_output("DISM /Online /Set-Edition:ServerStandard /ProductKey:" + key + " /AcceptEula /NoRestart");