前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#操作注册表,设置系统环境变量

C#操作注册表,设置系统环境变量

作者头像
望天
发布2018-08-02 11:42:47
9450
发布2018-08-02 11:42:47
举报
文章被收录于专栏:along的开发之旅

C#操作注册表,设置系统环境变量


起因

最近重装系统,NX9.0太大了,不想重装,原来的话重装LicenceServer后,NX9.0是可以直接打开的,但是.NET二次开发的时候有错误,这次试一下添加注册表后,可不可以进行二次开发.  需要添加的

UGII_BASE_DIR

UGII_LANG

UGII_ROOT_DIR

D:\Program Files\Siemens\NX 9.0

UGII_LANG

D:\Program Files\Siemens\NX 9.0\UGII

因为懒得进[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]添加,也是为了以后方便打算,所以写了一个C#操作注册表的控制台程序.

代码

添加一个叫SysEnvironment的类,用来封装各个操作函数.

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace 设置系统环境变量
{
 class SysEnvironment
 {
 /// <summary>
 /// 获取系统环境变量
 /// </summary>
 /// <param name="name">输入HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
 /// 下key的对应的字符串</param>
 /// <returns>Key对应的字符串</returns>
 public static string GetSysEnvironmentByName(string key)
 {
 string result = string.Empty;
 try
 {
                result = OpenSysEnvironment().GetValue(key).ToString();
 }
 catch (Exception)
 {

 return result;
 }
 return result;
 }

 /// <summary>
 /// 获得RegistryKey,值为
 /// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
 /// </summary>
 /// <returns></returns>
 public static RegistryKey OpenSysEnvironment()
 {
 RegistryKey regLocalMachine = Registry.LocalMachine;

 //打开HKEY_LOCAL_MACHINE下的SYSTEM,true表示具有写权限 
 //RegistryKey regSystem = regLocalMachine.OpenSubKey("System", true);
 //RegistryKey regControlSet = regSystem.OpenSubKey("CurrentControlSet", true).OpenSubKey("Control",true).OpenSubKey("Session Manager",true);
 RegistryKey regEnvironment = regLocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Session Manager\Environment", true);
 return regEnvironment;
 }

 /// <summary>
 /// 设置系统环境变量
 /// </summary>
 /// <param name="name">变量名</param>
 /// <param name="strValue">值</param>
 /// <param name="i">用来表示在路径首添加,还是在路径末尾添加</param>
 public static string SetSysEnvironment(string key, string strValue)
 {
 string pathList = null;

 if (CheckSysEnvironmentExist(key) == false)
 {
 //如果环境变量不存在,直接key-value添加就可
 OpenSysEnvironment().SetValue(key, strValue);
 return "已成功将所给key的环境变量设为value!";
 }
 else
 {
 //如果环境变量存在,需要首先检查value是否已经包含在key所对应的string中,
 //如果不包含在其中,就添加,否则,直接返回就可
                pathList = GetSysEnvironmentByName(key);
 if (pathList.Contains(strValue))
 {
 return "所给key的环境变量已包含value!";//已设置过,不用再次设置
 }
 if (!pathList.EndsWith(";"))
 {
 OpenSysEnvironment().SetValue(key, pathList + ";");
                    pathList = GetSysEnvironmentByName(key);
 }
 else
 {
 OpenSysEnvironment().SetValue(key, pathList + strValue);
 return "已成功在环境变量尾追加所给变量!";
 }
 }
 }

 /// <summary>
 /// 检测系统环境变量是否存在
 /// </summary>
 /// <param name="name">要修改path的Key</param>
 /// <returns></returns>
 public static Boolean CheckSysEnvironmentExist(string key)
 {
 if (!string.IsNullOrEmpty(GetSysEnvironmentByName(key)))
 {
 return true;
 }
 else
 {
 return false;
 }
 }
 }
}

Main函数里主要是读取输入的东西,并调用上述函数.

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设置系统环境变量
{
 class Program
 {
 static void Main(string[] args)
 {
 Console.WriteLine(@"请输入您要输入的键值对,用空格分割,如 Path C:\Go");
 string keyValue = Console.ReadLine();
 while (keyValue != "z")
 {
 string[] result = keyValue.Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries);
 string key = result[0];//这几行主要是处理输入路径中空格的情况,输入的是D:\Program Files\Siemens\NX 9.0\UGII,也不会因为空格而变成 D:\Program
 string value = keyValue.Trim().Remove(0, key.Length).Trim(' ');
 Console.WriteLine("key:{0} \t value:{1}", key, value);
 Console.WriteLine(SysEnvironment.SetSysEnvironment(key, value));
 Console.WriteLine("请输入下一键值对,按z加上回车结束程序");
                keyValue = Console.ReadLine();
 }
 Console.ReadKey();
 }
 }
}

参考博客:http://www.cnblogs.com/ITBread/archive/2012/04/15/2450367.html

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014年07月28日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C#操作注册表,设置系统环境变量
    • 起因
      • 代码
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档