您的位置:首页 > 编程语言 > C#

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

2014-07-28 22:39 417 查看



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


起因

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

需要添加的
UGII_BASE_DIRUGII_LANGUGII_ROOT_DIR
D:\Program Files\Siemens\NX 9.0UGII_LANGD:\Program Files\Siemens\NX 9.0\UGII
因为懒得进[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]添加,也是为了以后方便打算,所以写了一个C#操作注册表的控制台程序.


代码

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

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函数里主要是读取输入的东西,并调用上述函数.

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  注册表