Click here to Skip to main content
12,821,494 members (23,715 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 27 Oct 2010

Set System Env Variable Using .Net ManagementScope

, 29 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Use of .Net ManagementScope

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;
                //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);

                //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() + ";");
                                EnvVar["VariableValue"] = (object)(((string)EnvVar["VariableValue"]) + ";" + fileInfo.DirectoryName.ToString() + ";");
                            //Updates the 'Path' of the target machine.

            catch (Exception ex)

Happy Programming ! !

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


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


About the Author

Software Developer (Senior) Computer Sciences Corporation, Chennai
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 5 nice, thanks for sharing Pin
johannesnestler29-Oct-10 0:20
memberjohannesnestler29-Oct-10 0: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 29 Oct 2010
Article Copyright 2010 by mohanayyavu
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid