编写系统环境变量-->系统变量-->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);
}
}
}