GetEnvironmentVariable() and SetEnvironmentVariable() for PATH Variable

后端 未结 6 1649

I want to extend the current PATH variable with a C# program. Here I have several problems:

  1. Using GetEnvironmentVariable(\"PATH\", EnvironmentVariable

相关标签:
6条回答
  • 2020-12-11 15:36

    You can use the registry to read and update:

    string keyName = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment";
    //get non-expanded PATH environment variable            
    string oldPath = (string)Registry.LocalMachine.CreateSubKey(keyName).GetValue("Path", "", RegistryValueOptions.DoNotExpandEnvironmentNames);
    
    //set the path as an an expandable string
    Registry.LocalMachine.CreateSubKey(keyName).SetValue("Path", oldPath + ";%MYDIR%",    RegistryValueKind.ExpandString);
    
    0 讨论(0)
  • 2020-12-11 15:36

    Using Registry.GetValue will expand the placeholders, so I recommend using Registry.LocalMachine.OpenSubKey, then get the value from the sub key with options set to not expand environment variables. Once you've manipulated the path to your liking, use the registry to set the value again. This will prevent Windows "forgetting" your path as you mentioned in the second part of your question.

    const string pathKeyName = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment";
    var pathKey = Registry.LocalMachine.OpenSubKey(pathKeyName);
    var path = (string)pathKey.GetValue("PATH", "", RegistryValueOptions.DoNotExpandEnvironmentNames);
    // Manipulate path here, storing in path
    Registry.SetValue(String.Concat(@"HKEY_LOCAL_MACHINE\", pathKeyName), "PATH", path);
    
    0 讨论(0)
  • 2020-12-11 15:37

    While working on the application we had to have an option to use Oracle instantclient from user-defined folder. In order to use the instantclient we had to modify the environment path variable and add this folder before calling any Oracle related functionality. Here is method that we use for that:

        /// <summary>
        /// Adds an environment path segments (the PATH varialbe).
        /// </summary>
        /// <param name="pathSegment">The path segment.</param>
        public static void AddPathSegments(string pathSegment)
        {
            LogHelper.Log(LogType.Dbg, "EnvironmentHelper.AddPathSegments", "Adding path segment: {0}", pathSegment);
            string allPaths = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);
            if (allPaths != null)
                allPaths = pathSegment + "; " + allPaths;
            else
                allPaths = pathSegment;
            Environment.SetEnvironmentVariable("PATH", allPaths, EnvironmentVariableTarget.Process);
        }
    

    Note that this has to be called before anything else, possibly as the first line in your Main file (not sure about console applications).

    0 讨论(0)
  • 2020-12-11 15:38

    You could try this mix. It gets the Path variables from the registry, and adds the "NewPathEntry" to Path, if not already there.

    static void Main(string[] args)
        {
            string NewPathEntry = @"%temp%\data";
            string NewPath = "";
            bool MustUpdate = true;
            string RegKeyName = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment";
            string path = (string)Microsoft.Win32.Registry.LocalMachine.OpenSubKey(RegKeyName).GetValue
                ("Path", "", Microsoft.Win32.RegistryValueOptions.DoNotExpandEnvironmentNames);
            string[] paths = path.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string subPath in paths)
            {
                NewPath += subPath + ";";
                if (subPath.ToLower() == NewPathEntry.ToLower())
                {
                    MustUpdate = false;
                }
            }
            if (MustUpdate == true)
            {
                Environment.SetEnvironmentVariable("Path", NewPath + NewPathEntry, EnvironmentVariableTarget.Machine);
            }
        }
    
    0 讨论(0)
  • 2020-12-11 15:58

    You can use WMI to retrieve the raw values (not sure about updating them though):

    ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Environment WHERE Name = 'PATH'");
    foreach (ManagementBaseObject managementBaseObject in searcher.Get())
         Console.WriteLine(managementBaseObject["VariableValue"]);
    

    Check WMI Reference on MSDN

    0 讨论(0)
  • 2020-12-11 16:03

    You could go through the registry...

    string keyName = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment";
    //get raw PATH environment variable
    string path = (string)Registry.GetValue(keyName, "Path", "");
    
    //... Make some changes
    
    //update raw PATH environment variable
    Registry.SetValue(keyName, "Path", path);
    
    0 讨论(0)
提交回复
热议问题