我想用C#程序扩展当前的PATH变量。我有几个问题:
使用GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine)
替换占位符(即'%SystemRoot%\system32'
替换为当前路径'C:\Windows\system32'
)。更新PATH变量,我不想用路径替换占位符。
在SetEnvironmentVariable
之后,无法再从命令框中打开任何程序(即命令框中的calc.exe不起作用)。我使用以下代码:
String oldPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable("PATH", oldPath + ";%MYDIR%", EnvironmentVariableTarget.Machine);
使用Windows编辑和更改PATH
变量后,一切都可以正常工作。 (我需要更改内容,否则不会被覆盖)
答案 0 :(得分:5)
您可以使用注册表来阅读和更新:
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);
答案 1 :(得分:1)
您可以使用WMI检索原始值(不确定更新它们):
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Environment WHERE Name = 'PATH'");
foreach (ManagementBaseObject managementBaseObject in searcher.Get())
Console.WriteLine(managementBaseObject["VariableValue"]);
在MSDN上查看WMI Reference
答案 2 :(得分:1)
你可以尝试这种混合。它从注册表中获取Path变量,并将“NewPathEntry”添加到Path(如果尚未存在)。
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);
}
}
答案 3 :(得分:0)
你可以通过注册表......
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);
答案 4 :(得分:0)
在处理应用程序时,我们必须选择使用来自用户定义文件夹的Oracle instantclient。为了使用instantclient,我们必须修改环境路径变量,并在调用任何Oracle相关功能之前添加此文件夹。 这是我们使用的方法:
/// <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);
}
请注意,必须先调用它,可能是主文件中的第一行(不确定控制台应用程序)。
答案 5 :(得分:0)
使用Registry.GetValue
将展开占位符,因此我建议使用Registry.LocalMachine.OpenSubKey
,然后从子键中获取值,并将选项设置为不扩展环境变量。在您根据自己的喜好操作路径后,请使用注册表再次设置该值。这将阻止Windows&#34;忘记&#34;你在问题的第二部分提到的路径。
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);