GetEnvironmentVariable() and SetEnvironmentVariable() for PATH Variable

后端 未结 6 1664

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

提交回复
热议问题