有什么方法可以在 Process.Start(..) 期间保持外部命令窗口打开吗?

发布于 2024-07-22 01:37:51 字数 649 浏览 3 评论 0原文

我有以下运行 bat 文件的代码。 然后bat文件运行一些.exe..它会做一些事情。 整个过程大约需要 5-10 秒。

ProcessStartInfo start = new ProcessStartInfo
{
    Arguments = "\"" + newTargetFile + "\"" +
                " " +
                "\"" + originalFile.FullName + "\"",
    FileName = filename,
    WindowStyle = ProcessWindowStyle.Normal,
    CreateNoWindow = false,
    UseShellExecute = false
};

// Run the external process & wait for it to finish
using (Process proc = Process.Start(start))
{
    proc.WaitForExit();
}

我想做的是让命令窗口保持打开状态,即使在进程终止后也是如此。 有什么办法可以做到这一点吗?

否则,我可以将该窗口的所有输出发送到我的调试器,这样我就不必担心该窗口的剩余情况吗?

干杯。

i've got the following code which runs a bat file. the bat file then runs some .exe .. which does some stuff. The stuff takes aroun 5-10 seconds.

ProcessStartInfo start = new ProcessStartInfo
{
    Arguments = "\"" + newTargetFile + "\"" +
                " " +
                "\"" + originalFile.FullName + "\"",
    FileName = filename,
    WindowStyle = ProcessWindowStyle.Normal,
    CreateNoWindow = false,
    UseShellExecute = false
};

// Run the external process & wait for it to finish
using (Process proc = Process.Start(start))
{
    proc.WaitForExit();
}

What i'm trying to do is leave the command window open, even after the process terminates. Is there any way to do this?

Otherwise, can i get all the output of that window going to my debugger instead, so i don't need to worry about this window remaining?

cheers.

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

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

发布评论

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

评论(2

转瞬即逝 2024-07-29 01:37:51

如果您使用 /k 参数运行 cmd 解释器并传入批处理文件名作为下一个参数,您应该得到您想要的结果。 /k 参数告诉 Windows 在完成您作为下一个参数指定的操作后让 cmd 解释器保持打开状态。

您当然也可以使用 RedirectStandardOutput 属性,但我认为最终会更复杂。

If you run the cmd interpreter with a /k argument and pass in the batch file name as the next argument, you should get what you're after. The /k argument tells Windows to leave the cmd interpreter open when it's done with whatever you tell it to do as the next argument.

You could certainly also use the RedirectStandardOutput property, but I think that would be more complicated in the end.

标点 2024-07-29 01:37:51

尝试使用 /k 参数来保持终端打开:

    System.Diagnostics.Process proc = new System.Diagnostics.Process();

    string path = System.IO.Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName;
    if (Environment.OSVersion.Version.Major >= 6)
    {
        path = System.IO.Directory.GetParent(path).ToString();
    }

    proc.StartInfo.WorkingDirectory = path;
    proc.StartInfo.FileName = "cmd.exe";
    proc.StartInfo.Arguments = "/k git --version";
    proc.Start();

Try something like this with a /k argument to keep open the terminal:

    System.Diagnostics.Process proc = new System.Diagnostics.Process();

    string path = System.IO.Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName;
    if (Environment.OSVersion.Version.Major >= 6)
    {
        path = System.IO.Directory.GetParent(path).ToString();
    }

    proc.StartInfo.WorkingDirectory = path;
    proc.StartInfo.FileName = "cmd.exe";
    proc.StartInfo.Arguments = "/k git --version";
    proc.Start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文