65.9K
CodeProject is changing. Read more.
Home

Set System Env Variable Using .Net ManagementScope

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Oct 27, 2010

CPOL
viewsIcon

14690

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