Peter Viola
  • Linkedin
  • Twitter
  • RSS
  • IIS
  • ASP.NET
  • Windows Server
    • Windows Server 2012
    • Windows Server 2008
    • SQL Server
  • WordPress
  • Email
  • Windows 10
Search the site...
  • Home
  • ASP.NET
  • Using C# to Modify Windows Server Environment Variables

Using C# to Modify Windows Server Environment Variables

September 27, 2020 / Peter Viola / ASP.NET, Windows Server
Share on Facebook Share
Share on Twitter Tweet
Share on Pinterest Share
Share on Linkedin Share
Share on Digg Share

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.

Avatar

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

asp.net, c#, Windows Server

Recent Posts

  • Windows 10 Data Recovery – Getting Files Back After a Crash
  • Windows Low Disk Space – Easy Drive Cloning With Macrium Reflect
  • Using C# to Automate Java Keytool Certificate Signing Requests
  • Using C# to Modify Windows Server Environment Variables
  • How to Integrate a Mailchimp Mailing List with WordPress

Categories

  • ASP.NET
  • Email
  • IIS
  • SmarterTools
  • SQL Server
  • Windows 10
  • Windows Server
  • Windows Server 2008
  • Windows Server 2012
  • Wordpress

Tags

301 redirect Advanced Intellect asp.net backups c# disk space DTA Dynamic IP Restrictions email email security Fiddler FTP FTP Security IIS IIS 6 IIS 7 IIS 8 Log Parser optimization Performance PHP Powershell SEO Server Core Smartermail Smartertools SMTP spam filtering SQL Injection SQL Profiler SQL Server SSL StressStimulus Task Scheduler Url Rewrite vbscript robocopy vmware Web PI Windows 8 Windows 10 Windows Server 2008 Windows Server 2012 Wordpress wpforms WPI
(c) 2020 Peter Viola
  • Home
  • Privacy Policy
  • Affiliate Disclaimer
  • IIS
  • ASP.NET
  • Email
  • WordPress
  • Windows 10
  • SmarterTools
  • Windows Server
X
Subject:
Message: