0

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.

Luke Vo
  • 17,859
  • 21
  • 105
  • 181
  • 1
    Imho. UseShellExecute schuld do the Job.. could you try this: var startInfo = new ProcessStartInfo("code.exe", this.Folder); startInfo.UseShellExecute = true; Process.Start(startInfo); – Starbax Feb 22 '22 at 19:50
  • @Starbax I tried but it doesn't make any difference – Luke Vo Feb 22 '22 at 20:45
  • Not sure if object initializer and constructor work well together in this case. You could try: var procInfo = new ProcessStartInfo(); procInfo.FileName = "code.exe"; procInfo.Arguments = this.Folder; procInfo.UseShellExecute = true; – Starbax Feb 22 '22 at 21:05
  • Opening Notepad++ works just fine for me via .Net 5 console app/ winForms app: var procInfo = new ProcessStartInfo("\"C:\\Program Files\\Notepad++\\notepad++.exe\"") { UseShellExecute = true }; Process.Start(procInfo); Maybe you can try this call, without arguments, first. – Starbax Feb 22 '22 at 21:11
  • Unfortunately the first suggestion didn't work. I can open Notepad++ and it doesn't get closed when my app exits and even if I pass a parameter, that's interesting. – Luke Vo Feb 23 '22 at 00:07

0 Answers0