Dotnet startup process passes in nonexistent folder as working directory behavior change

Time:2022-6-1

This article records that under dotnet, start the process, and pass in the nonexistent folder as the working directory of the process, respectively Net framework and Net core behavior

Under dotnet 6, processstartinfo can be used to help create process processes. For example, the following code is used to test, and the nonexistentZ:\Windowsfolder

Console.WriteLine($"Fx {Environment.CurrentDirectory}");

if (args.Length > 0)
{
    return;
}

var location = Assembly.GetExecutingAssembly().Location;
var fileName = Path.GetFileNameWithoutExtension(location);
var directory = Path.GetDirectoryName(location);

var exe = Path.Combine(directory, fileName + ".exe");
var processStartInfo = new ProcessStartInfo(exe,"fx")
{
    WorkingDirectory = "Z:\\Windows"
};
var process = Process.Start(processStartInfo);

The run will run in process Throw system Componentmodel Win32exception says the directory name is invalid

If you are in an English environment, you will be promptedThe directory name is invalidThus failed

However, if the workingdirectory working path of processstartinfo is not set, the environment of the current process will be used by default Currentdirectory value as the working path of the startup process

In Net core and Net framework, when starting, set usehellexecute to true and false respectively. The behavior is different. Without setting the workingdirectory working path of processstartinfo, let the new process use environment. by default Currentdirectory working folder. However, this working path is the path of a plugged in U disk, as shown in the following code

Environment.CurrentDirectory = @"I:\";

            var exe = Path. Combine(directory, fileName + ".exe"); //  When this code is executed, pull out the U disk and make i:\ nonexistent
            var processStartInfo = new ProcessStartInfo(exe, "fx")
            {
                Usehellexecute = true, // also set to false
            };
            var process = Process.Start(processStartInfo);
            process.WaitForExit();

I use Net 6 and Net framework 4.5. The tests are as follows:

In Net core, set the value of usehellexecute=false. The running result is: success. The working path of the new process is equal toI:\route

In Net core, set the value of usehellexecute=true. The running result is: success. The working path of the new process is equal toC:\Windowsroute

In Net framework, set the value of usehellexecute=false, and the running result is: run process Start failed, promptSystem Componentmodel Win32exception: 'the directory name is invalid.'error

In Net framework, set the value of usehellexecute=true. The running result is: success. The working path of the new process is equal toC:\Windowsroute

See morec# – Win32Exception: The directory name is invalid – Stack Overflow

CreateProcess and ShellExecute differences – Stack Overflow

All the code in this article is placed ingithubandgiteeWelcome to visit

You can obtain the source code of this article in the following ways: first create an empty folder, then use the command line CD command to enter this empty folder, and enter the following code in the command line to obtain the code of this article

git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin f7696a3e9f33dfcbfdd8ab92afaa77ab668dfeb9

The above uses the source of gitee. If gitee cannot access it, please replace it with the source of GitHub

git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git

After obtaining the code, enter the hebarlawkukekebuwagay folder