如何在执行后保持控制台窗口打开?
这是一个有点复杂的问题。我尝试过所有可能的事情,但仍然没有工作。我运行CMD运行WinForm应用程序,然后在cmd上运行另一个应用程序(控制台应用程序)。它在/c START xyz
上工作,但是当应用程序结束时CMD总是关闭。我想暂停这个窗口。如何在执行后保持控制台窗口打开?
ProcessStartInfo processInfo = new ProcessStartInfo {
FileName = "cmd.exe",
WorkingDirectory = Path.GetDirectoryName(YourApplicationPath),
Arguments = "/K START " + cmdparametr,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
CreateNoWindow = false,
UseShellExecute = false,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
};
Process p = new Process {
StartInfo = processInfo
};
p.Start();
int ExitCode;
p.WaitForExit();
// *** Read the streams ***
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
ExitCode = p.ExitCode;
MessageBox.Show("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output));
MessageBox.Show("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error));
MessageBox.Show("ExitCode: " + ExitCode.ToString(), "ExecuteCommand");
p.Close();
ReadStream
工作当我添加参数:START /b
但我认为这并不重要。 WaitForExit()
不起作用。
是否有可能通过命令暂停应用可能是这样的: /k start xyz.exe & PAUSE
?
我的应用程序是控制台应用程序!
可以使用pause
-command内C#如果你想: 我用它如下:
解决方案№1:
//optional: Console.WriteLine("Press any key ...");
Console.ReadLine(true);
解决方案№2:(用途P/Invoke)
// somewhere in your class
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, SetLastError=true)]
public static extern int system(string command);
public static int Main(string[] argv)
{
// your code
system("pause"); // will automaticly print the localized string and wait for any user key to be pressed
return 0;
}
编辑:您可以动态创建一个临时批处理文件,并执行它,例如:
string bat_path = "%temp%/temporary_file.bat";
string command = "command to be executed incl. arguments";
using (FileStream fs = new FileStream(bat_path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read))
using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
{
sw.WriteLine("@echo off");
sw.WriteLine(command);
sw.WriteLine("PAUSE");
}
ProcessStartInfo psi = new ProcessStartInfo() {
WorkingDirectory = Path.GetDirectoryName(YourApplicationPath),
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
CreateNoWindow = false,
UseShellExecute = false,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
};
Process p = new Process() {
StartInfo = psi;
};
p.Start();
int ExitCode;
p.WaitForExit();
// *** Read the streams ***
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
ExitCode = p.ExitCode;
MessageBox.Show("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output));
MessageBox.Show("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error));
MessageBox.Show("ExitCode: " + ExitCode.ToString(), "ExecuteCommand");
p.Close();
File.Delete(bat_path);
不包括START
命令,使用这样的
processInfo.Arguments = "/K " + your_console_app_exe_path_and_args;
确保在需要的地方附上双引号。
没有,没有参数'START'我没有看到控制台应用程序的任何结果。 – test
这是因为您正在重定向您的控制台应用程序的输出。 –
您是否尝试过将'&pause'作为您的问题记录添加到命令的末尾?也许相关 - 是否有任何理由重定向输入? –