Search code examples
c#.net.net-coreprocessconsole

Creating new .Net Core Console Process: Exception when using environment variables with UseShellExecute=true


I want to run a number of .Net Core console apps from a single console. I have the following code:

        var startInfo = new ProcessStartInfo("c:\path\to\filename.exe", "arguments")
        {
            UseShellExecute = true,
            WorkingDirectory = "c:\working\directory",
        };
        var process = Process.Start(startInfo);

I receive the following exception: System.InvalidOperationException: 'The Process object must have the UseShellExecute property set to false in order to use environment variables.'

Apps use variables from appsettings.json file in WorkingDirectory.

How to run the app processes successfully, each in a separate console?


Solution

  • This may help

    var startInfo = new ProcessStartInfo("cmd.exe", @"/K ""c:\path\to\filename.exe"" arguments")
    {
        UseShellExecute = false,
        WorkingDirectory = @"c:\working\directory",
        //CreateNoWindow = true
    };
    var process = Process.Start(startInfo);