Using C# to Modify Windows Server Environment Variables

Recently I was working on an issue where the path to an application needed to be in the server Path Environment Variable. I didn’t have physical access to the server and needed an easy way to get the path updated so I created a simple Asp.net Windows Form to accomplish this task.

Change Windows Environment Variables

Environment Variables contain specific information about the server and are accessible by any application or process on the server such as Path. On a windows server you can manually modify Environment Variables by going to the Control Panel opening the System Properties and then selecting Environment Variables.

image

 

Clicking Edit allows you to change existing variables or clicking New allows you to create a new variable.

image

 

Updating windows path using C#

I decided to use a .net Win Form to modify the windows path dynamically because I didn’t have access to the server with the issue and wasn’t even sure of the path to the application in question. This way the person who had access to the server just needed to run my utility from correct application path and then click a button to update the path environment variable. As shown in the picture below when the program opens it shows the current path and validates if it’s in the system path.

image

 

To quickly see the current system path just open a command prompt and execute the Path command. This will output the entire path environment variable contents. On some servers this could be quite a lot of info so it may be easier to read by redirecting the output to a text file.

With C#, to get the current path use Directory.GetCurrentDirectory() and to check the system path use Environment.GetEnvironmentVariable(“PATH”). So here’s what my program does when it loads.

 

string myPath = Directory.GetCurrentDirectory();

labelCurrentPath.Text = myPath;

if (Environment.GetEnvironmentVariable("PATH").Contains(myPath))
{
labelSysPath.Text = "..in system PATH";
labelSysPath.ForeColor = System.Drawing.Color.Green;
}
else
{
labelSysPath.Text = "...not in system PATH";
labelSysPath.ForeColor = System.Drawing.Color.Red;
}

 

Clicking the button on the form will set the current path that was identified earlier using the Environment.SetEnvironment method. I want my path update to be at the front of the list so I just concatenate the new path value to the existing path. However from testing I discovered that new setting would be lost after logging off the server so be sure to set the target using the EnvironmentVariableTarget overload.

 

Environment.SetEnvironmentVariable("PATH", myPath + ";" + sysPath, EnvironmentVariableTarget.Machine);

 

After the program runs and the the setting the has been applied, I manually check the system variables again and can see the path has been correctly updated as shown below.

image

In Summary

Using C# and asp.net it’s easy to manipulate Windows server environment variables with the Environment.SetEnvironment method. Thanks for reading.

Peter Viola

Creative, customer focused, results oriented, Senior Web Systems Engineer who enjoys providing the highest level of customer service supporting complex Windows hosting solutions. MCITP, MCSA, MCTS

More Posts - Website