繁体   English   中英

执行后如何保持控制台窗口打开?

[英]How to keep Console Window open after execution?

这是一个有点复杂的问题。 我可能已经尝试了一切,但仍然无法正常工作。 我从运行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();

当我添加参数START /b时, ReadStream可以工作,但是我认为这并不重要。
WaitForExit()不起作用。

是否可以通过以下命令暂停应用程序: /k start xyz.exe & PAUSE


我的应用程序是控制台应用程序!

如果需要,可以在C#中使用pause -command:我按如下方式使用它:

解决方案№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); 

为了防止关闭控制台应用程序,您可以使用:

Console.ReadLine();

它将等待任何键,并且不会立即关闭。

不包括START命令,使用类似这样的东西

processInfo.Arguments = "/K " + your_console_app_exe_path_and_args;

确保在需要时用双引号引起来。

暂无
暂无

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

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