Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit ef389bb

Browse files
author
William Li
authored
Add null check for Environment.GetEnvironmentVariable (#8970)
1 parent 0ca2c9f commit ef389bb

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/dotnet/ShellShim/WindowsEnvironmentPath.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,26 @@ public void AddPackageExecutablePathToUserPath()
3333

3434
var existingUserEnvPath = Environment.GetEnvironmentVariable(PathName, EnvironmentVariableTarget.User);
3535

36-
if (existingUserEnvPath.EndsWith(';'))
36+
if (existingUserEnvPath == null)
3737
{
38-
existingUserEnvPath = existingUserEnvPath.Substring(0, (existingUserEnvPath.Length - 1));
38+
Environment.SetEnvironmentVariable(
39+
PathName,
40+
_packageExecutablePath,
41+
EnvironmentVariableTarget.User);
3942
}
43+
else
44+
{
45+
if (existingUserEnvPath.EndsWith(';'))
46+
{
47+
existingUserEnvPath = existingUserEnvPath.Substring(0, (existingUserEnvPath.Length - 1));
48+
}
49+
50+
Environment.SetEnvironmentVariable(
51+
PathName,
52+
$"{existingUserEnvPath};{_packageExecutablePath}",
53+
EnvironmentVariableTarget.User);
4054

41-
Environment.SetEnvironmentVariable(
42-
PathName,
43-
$"{existingUserEnvPath};{_packageExecutablePath}",
44-
EnvironmentVariableTarget.User);
55+
}
4556
}
4657

4758
private bool PackageExecutablePathExists()

0 commit comments

Comments
 (0)