如何启动控制台应用程序并防止其最终关闭?

发布于 2024-12-26 06:57:05 字数 596 浏览 2 评论 0原文

我正在从 .NET 应用程序启动 shell 脚本 (.cmd)。我希望控制台窗口在脚本完成后保持打开状态,以便我可以检查是否有任何错误。我正在尝试使用 cmd.exe /K:my.cmd,但由于某种原因不起作用(脚本未执行)。即使是简单的“dir”命令也不起作用。还有其他想法吗?

为了演示,这个:

System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
{
    Arguments = "/K:dir",
    ErrorDialog = true,
    FileName = "cmd.exe",
    WorkingDirectory = @"C:\"
});

给我这个:

C:\>

添加说明:我的应用程序(启动脚本的应用程序)是一个Windows 表单应用程序。执行上述代码将打开一个新的控制台窗口(正如它应该的那样)。我希望新的控制台窗口保持打开状态。另外,我无法修改脚本。这是一个自动构建脚本,必须终止。

I'm starting a shell script (.cmd) from my .NET application. I'd like the console window to remain open after the script has completed so that I can check for any errors. I'm trying with cmd.exe /K:my.cmd, but for some reason that does not work (the script does not get executed). Even a simple "dir" command does not work. Any other ideas?

To demonstrate, this:

System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
{
    Arguments = "/K:dir",
    ErrorDialog = true,
    FileName = "cmd.exe",
    WorkingDirectory = @"C:\"
});

Gives me this:

C:\>

Added clarification: My application (the one that starts the script) is a windows forms application. Executing the above code opens a new console window (as it should). I want THAT new console window to remain open. Also, I cannot modify the script. It's an automated build script and it HAS to terminate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笛声青案梦长安 2025-01-02 06:57:05

我不知道这是否是整个问题,但如果您运行 cmd /K:dir,则不会运行任何内容。使用空格而不是冒号使其对我有用。

即,cmd /K dir


还值得注意的是,您可以在运行对话框中测试这些命令。在任何最新的 Windows 版本中,可以通过按住 Win 并按 R 来完成此操作。

I don't know if this is the whole problem, but if you run cmd /K:dir, nothing is run. Using a space instead of a colon makes it work for me.

i.e., cmd /K dir


It's also worth noting that you can test these commands in the run dialog. In any recent Windows version this can be done by holding down Win and pressing R.

命硬 2025-01-02 06:57:05

从示例中删除冒号:

System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
{
  Arguments = "/K dir",
  ErrorDialog = true,
  FileName = "cmd.exe",
  WorkingDirectory = @"C:\"
});

Remove the colon from your example:

System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
{
  Arguments = "/K dir",
  ErrorDialog = true,
  FileName = "cmd.exe",
  WorkingDirectory = @"C:\"
});
春夜浅 2025-01-02 06:57:05

您可以将命令放入批处理 (.bat) 文件中,并使批处理文件中的最后一个命令“暂停”(不带引号)。然后,不执行cmd,而是执行批处理文件。

示例批处理文件 - test.bat

目录
暂停

您还可以将结果输出到文本文件,然后在应用程序中显示该文本文件。

DIR >results.txt

You could place your commands in a batch (.bat) file, and make the last command within the batch file "pause" (without quotes). Then, instead of executing cmd, execute the batch file instead.

Sample batch file - test.bat

dir
pause

You could also output the results to a text file, then show the text file within your application.

DIR >results.txt
守不住的情 2025-01-02 06:57:05

我会将错误记录到文件中而不是保持窗口打开。
要保持 .net 控制台窗口打开,请添加

Console.ReadLine();

到末尾,这将要求用户按 Enter 才能继续。 如果您想保持进程窗口打开,您还可以使用 Console.ReadKey() 使用任意键,

命令是 pause

I would log errors to a file rather than keep the window open.
to keep the .net console window open add

Console.ReadLine();

to the end, this will require the user to press Enter to continue. you can also use Console.ReadKey() to use any key

if you want to keep the Process windows open the command is pause

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文