Click here to Skip to main content
15,885,767 members
Articles / Programming Languages / C#
Tip/Trick

Set System Env Variable Using .Net ManagementScope

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
29 Oct 2010CPOL 14.5K   4   1
Use of .Net ManagementScope
Hi,

I had to Get/Set env variables of a remote/local computer in my project under given credentials with the help of ManagementScope.But, Didn't get any proper help and hence posting this one just for other programmers...


//Includes a new path to the System(Local/Remote) Environment variable 'Path'
       private void SetEnvPath(bool isRemote, string MachineName, string _UserName, string _Password, FileInfo fileInfo)
       {
           ManagementObjectSearcher query = null;
           ManagementObjectCollection queryCollection = null;
           ConnectionOptions _COption = new ConnectionOptions();
           //If the Target Computer is not local, Supply credentials and enable user privilages if required.
           if (isRemote)
           {
               _COption.Username = _UserName;
               _COption.Password = _Password;
               _COption.EnablePrivileges = true;
           }
           try
           {
               //Create Management scope for the target machine
               ManagementScope _Scope = new ManagementScope("\\\\" + MachineName + "\\root\\cimv2", _COption);
               //Create a management Class with ManagementPath
               //Win32_Environment, To get all Environment variables of Target computer.< Win32_Environment:Path > will get only 'Path' variable.
               //ManagementClass _Class = new ManagementClass(_Scope, new ManagementPath("Win32_Environment:Path"), new ObjectGetOptions());
               _Scope.Options.Timeout = TimeSpan.FromSeconds(5);
               _Scope.Connect();

               //Query to be run against target computer.
               //This will select only 'Path' varibles from all Environment variables.
               ObjectQuery setQuery = new ObjectQuery("SELECT * FROM Win32_Environment WHERE name='Path'");
               query = new ManagementObjectSearcher(_Scope, setQuery);
               queryCollection = query.Get();

               string test = "";
               if (queryCollection.Count > 0)
               {
                   foreach (ManagementObject EnvVar in queryCollection)
                   {
                       test = EnvVar.GetText(TextFormat.Mof);
                       if (test.Contains("Path") && !EnvVar["VariableValue"].ToString().Contains(fileInfo.DirectoryName))
                       {
                           //EnvVar["VariableValue"] - Value of the Environment variable 'Path'
                           if (EnvVar["VariableValue"].ToString().EndsWith(";"))
                               EnvVar["VariableValue"] = (object)(((string)EnvVar["VariableValue"]) + fileInfo.DirectoryName.ToString() + ";");
                           else
                               EnvVar["VariableValue"] = (object)(((string)EnvVar["VariableValue"]) + ";" + fileInfo.DirectoryName.ToString() + ";");
                           //Updates the 'Path' of the target machine.
                           EnvVar.Put();
                       }
                   }
               }
           }

           catch (Exception ex)
           {

           }
       }





Happy Programming ! !
:laugh:




Mohan Ayyavu,
Senior Software Engineer,
Computer Sciences Corporation, Chennai, India

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Computer Sciences Corporation, Chennai
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 nice, thanks for sharing Pin
johannesnestler28-Oct-10 23:20
johannesnestler28-Oct-10 23:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.