gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:58:30 25 4
gpt4 key购买 nike

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


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

最佳答案

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

解决方案一:

//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);

关于c# - 执行后如何保持控制台窗口打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32954153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com