Support for empty environment variables

Support was added to be able to set an environment variable to the empty string using Environment.SetEnvironmentVariable(String, String). As part of this work, the behavior of setting the ProcessStartInfo.Environment and ProcessStartInfo.EnvironmentVariables properties was changed to be consistent with that of Environment.SetEnvironmentVariable(String, String).

Previous behavior

Previously:

  • Both Environment.SetEnvironmentVariable("TEST", string.Empty) and Environment.SetEnvironmentVariable("TEST", null) deleted the environment variable.
  • Both ProcessStartInfo.Environment["TEST"] = string.Empty and ProcessStartInfo.Environment["TEST"] = null set the environment variable in the child process to an empty value.

New behavior

Starting in .NET 9:

  • Environment.SetEnvironmentVariable("TEST", string.Empty) sets the environment variable value to an empty value. Environment.SetEnvironmentVariable("TEST", null) behavior is unchanged, that is, it still deletes the environment variable.
  • ProcessStartInfo.Environment["TEST"] = null deletes the environment variable. ProcessStartInfo.Environment["TEST"] = string.Empty behavior is unchanged, that is, it still sets the environment variable to an empty value.

Version introduced

.NET 9 Preview 6

Type of breaking change

This change is a behavioral change.

Reason for change

Before this change, it wasn't possible to use Environment.SetEnvironmentVariable(String, String) to set an environment variable to an empty value, which is a valid environment variable value on all supported platforms.

To delete an environment variable using Environment.SetEnvironmentVariable(String, String), change your code to pass null instead of string.Empty as the value argument.

To set the environment variable to an empty value using ProcessStartInfo.Environment or ProcessStartInfo.EnvironmentVariables, change your code to set these properties to string.Empty instead of to null.

Affected APIs