当前位置: 首页 > news >正文

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

http://www.lryc.cn/news/429152.html

相关文章:

  • Unity | Shader基础知识(第二十二集:两次渲染)
  • 鸿蒙Harmony实战开发:Arkts构造函数
  • @vueuse/core使用useColorMode实现主题颜色切换
  • 生信分析入门:从基础知识到实践操作的全方位指南
  • 【STM32 FreeRTOS】内存管理
  • vue3+vite+cesium配置参考
  • WEB应用服务器TOMCAT
  • maven打包jar后运行提示“没有主清单属性”问题的几种解决方案
  • 计算机毕业设计选题推荐-民宿可视化分析-Python爬虫-随机森林算法
  • WKWebView加载项目中网页的资源图片路径异常
  • 算法全面剖析
  • tp5php7.4配置sqlserver问题汇总
  • C语言随笔:字面量
  • chainlit的基本概念聊天对话中的元素
  • 【LeetCode:3】无重复字符串的最长子串(Java)
  • C#对字典使用Linq查询
  • 【Vue】Vue基础
  • 贪心 + 分层图bfs,newcoder 76652/B
  • 如何在Linux上部署Java Web应用程序
  • SpringBoot 整合 Excel 轻松实现数据自由导入导出
  • PyTorch 基础学习(13)- 混合精度训练
  • Mycat分片-垂直拆分
  • 一元四次方程求解-【附MATLAB代码】
  • 【极限性能,尽在掌控】ROG NUC:游戏与创作的微型巨擘
  • Ecosmos开启公测,将深度赋能CIOE中国光博会元宇宙参会新体验
  • 【Kubernetes】k8s集群之包管理器Helm
  • 嵌入式linux系统镜像制作day3(构建镜像)
  • 【生日视频制作】教师节中秋节国庆节车模特美女举牌AE模板修改文字软件生成器教程特效素材【AE模板】
  • RongCallKit iOS 端本地私有 pod 方案
  • C++11:可变参数模板