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

问题描述 投票:0回答:3

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


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

c# windows batch-file cmd
3个回答
4
投票

如果需要,您可以在 C# 中使用

pause
命令: 我的使用方法如下:

解决方案 1: (使用 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 automatically print the localized string and wait for any user key to be pressed
    return 0;
}

解决方案#2:

Console.WriteLine("Press any key to exit...");
Console.ReadLine(true);

**编辑**:您可以动态创建临时批处理文件并执行它,例如:
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);

1
投票

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

Console.ReadLine();

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


1
投票

不要包含

START
命令,使用类似这样的命令

processInfo.Arguments = "/K " + your_console_app_exe_path_and_args;

确保在需要的地方用双引号引起来。

© www.soinside.com 2019 - 2024. All rights reserved.