本文主要是介绍C# - 如何在Windows系统中通过C#添加新的PATH条目至系统和用户环境变量,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
编写系统环境变量-->系统变量-->path-->添加新的列
01:直接写
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace updatesystempath01
{class Program{static void Main(string[] args){const string targetPath = @"c:\aa"; // 目标路径try{// 获取当前系统的PATH环境变量string originalPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);// 检查PATH中是否已存在目标路径bool hasTargetPath = originalPath.Split(';').Any(path => path.Trim().ToLower() == targetPath.ToLower());if (!hasTargetPath){// 创建新PATH,将目标路径添加到原PATH后面string newPath = originalPath + ";" + targetPath;// 更新系统环境变量using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true)){//key.SetValue("PATH", newPath, RegistryValueKind.ExpandString);//Console.WriteLine("系统变量更新成功.");if (key != null){key.SetValue("PATH", newPath, RegistryValueKind.ExpandString);Console.WriteLine("系统变量更新成功.");}else{Console.WriteLine("未能获取注册表键.");}}}else{Console.WriteLine("系统变量已包含目标路径.");}}catch (Exception ex){Console.WriteLine("系统变量更新出错: " + ex.Message);}// 执行完所有代码后立即退出程序Environment.Exit(0);}}
}
02:把新添加的内容添加进path.txt中,方便后面修改
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace updatesystempath01
{class Program{static void Main(string[] args){// 尝试从文本文件读取目标路径string targetPathFromText;using (StreamReader reader = new StreamReader("path.txt")){targetPathFromText = reader.ReadLine();if (string.IsNullOrEmpty(targetPathFromText)){Console.WriteLine("文本文档为空或无法读取路径.");return;}}try{string originalPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);bool hasTargetPath = originalPath.Split(';').Any(path => path.Trim().ToLower() == targetPathFromText.ToLower());if (!hasTargetPath){string newPath = originalPath + ";" + targetPathFromText;using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true)){if (key != null){key.SetValue("PATH", newPath, RegistryValueKind.ExpandString);Console.WriteLine("系统变量更新成功.");}else{Console.WriteLine("未能获取注册表键.");}}}else{Console.WriteLine("系统变量已包含目标路径.");}}catch (Exception ex){Console.WriteLine("系统变量更新出错: " + ex.Message);}// 程序结束Console.WriteLine("程序执行完毕。");// 执行完所有代码后立即退出程序Environment.Exit(0);}}
}
文本文档 (path.txt
) 内容
确保path.txt
文件位于你的项目目录中,并且内容为:
复制插入
c:\aa
编写系统环境变量-->用户变量-->path-->添加新的列
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace updatesystempath001
{class Program{static void Main(string[] args){const string targetPath = @"c:\aa"; // 目标路径// 获取当前系统的PATH环境变量string originalPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);// 检查PATH中是否已存在"c:\aa"bool hasTargetPath = originalPath.Split(';').Any(path => path.Trim().ToLower() == targetPath);if (!hasTargetPath){// 创建新PATH,将"c:\aa"添加到原PATH后面// 创建新PATH,将目标路径添加到原PATH后面string newPath = originalPath + ";" + targetPath;// 更新系统环境变量try{using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Environment", true)) // 或者使用Registry.LocalMachine,取决于你要修改哪个用户的PATH{key.SetValue("PATH", newPath, RegistryValueKind.ExpandString);}}catch (Exception ex){Console.WriteLine("Error updating PATH: " + ex.Message);}}// 执行完所有代码后立即退出程序Environment.Exit(0);}}
}
这篇关于C# - 如何在Windows系统中通过C#添加新的PATH条目至系统和用户环境变量的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!