启动一个单独的进程,而不将其输出打印到控制台并在程序结束时终止它

发布于 2025-01-09 04:07:31 字数 1178 浏览 1 评论 0原文

我有以下 .NET (Core) 6 代码来打开 Windows 中 VS Code 中的文件夹(我的应用程序不需要在其他环境中运行):

    public void OpenInVsCode()
    {
        Console.WriteLine("Opening folder in VS Code: " + this.Folder);

        var procInfo = new ProcessStartInfo("code.exe", this.Folder)
        {
            // These 3 lines don't seem to matter
            RedirectStandardOutput = false,
            RedirectStandardInput = false,
            RedirectStandardError = false,
            // This line should solve the problem but it doesn't
            UseShellExecute = true,
        };
        Process.Start(procInfo);
    }

VS Code 确实启动了。但是,它开始将其输出打印到我的控制台应用程序中,当我的控制台应用程序关闭时,VS Code 也会关闭,这似乎与 这个答案相矛盾

输入图片此处描述

我如何启动一个完全独立的流程?


更新:这似乎很奇怪,在我的控制台应用程序中,如果它自行结束(即到达最后一行),即使我关闭完成的控制台窗口,应用程序也会停止并且不会关闭 VS Code。但是,如果应用程序保持打开状态(由于 Console.ReadLineawait Task.Delay),并且我通过关闭控制台窗口或按 Ctrl + C 来关闭它,则 VS Code 会终止。

I have the following .NET (Core) 6 code to open a folder in VS Code in Windows (my app doesn't need to run on other environments):

    public void OpenInVsCode()
    {
        Console.WriteLine("Opening folder in VS Code: " + this.Folder);

        var procInfo = new ProcessStartInfo("code.exe", this.Folder)
        {
            // These 3 lines don't seem to matter
            RedirectStandardOutput = false,
            RedirectStandardInput = false,
            RedirectStandardError = false,
            // This line should solve the problem but it doesn't
            UseShellExecute = true,
        };
        Process.Start(procInfo);
    }

VS Code does start. However it starts printing its output into my console app and when my Console app closes, VS Code is closed as well, which seems contradicting to this answer.

enter image description here

How do I start a totally separated Process?


Update: It seems strange that in my Console app if it ends by itself (i.e. reaches to the last line), the app stops and will not close VS Code even if I close the finished Console window. However if the app stays open (due to Console.ReadLine or await Task.Delay) and I close it by closing the Console window or pressing Ctrl + C, VS Code is terminated.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文