Click here to Skip to main content
Click here to Skip to main content

Tagged as

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
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 | :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)

Share

About the Author

mohanayyavu
Software Developer (Senior) Computer Sciences Corporation, Chennai
India India
No Biography provided
Follow on   Twitter

Comments and Discussions

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

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 29 Oct 2010
Article Copyright 2010 by mohanayyavu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid