private static void SetEnvironmentVariableCore(string variable, string value)
{
if (!Interop.Kernel32.SetEnvironmentVariableW(variable, value))
{
int errorCode = Marshal.GetLastWin32Error();
switch (errorCode)
{
case Interop.Errors.ERROR_ENVVAR_NOT_FOUND: // Allow user to try to clear a environment variable
return;
case Interop.Errors.ERROR_FILENAME_EXCED_RANGE: // Fix inaccurate error code from Win32
throw new ArgumentException(SR.Argument_LongEnvVarValue, nameof(value));
default:
throw new ArgumentException(Interop.Kernel32.GetMessage(errorCode));
}
}
}