C# 实现设置系统环境变量设置
以前实现系统环境变量设置时是要在电脑属性--高级--环境变量设置,实现方式主要有2种,
- 修改注册表,添加环境变量
- 调用系统Kernel32.DLL函数,设置环境变量
注册表方式,是要修改注册表的位置是[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
代码我已经封装,注意要引入命名空间
using Microsoft.Win32;
using System.Runtime.InteropServices;
如下:
class SysEnvironment
{
/// <summary>
/// 获取系统环境变量
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string GetSysEnvironmentByName(string name)
{
string result = string.Empty;
try
{
result = OpenSysEnvironment().GetValue(name).ToString();//读取
}
catch (Exception)
{
return string.Empty;
}
return result;
}
/// <summary>
/// 打开系统环境变量注册表
/// </summary>
/// <returns>RegistryKey</returns>
private static RegistryKey OpenSysEnvironment()
{
RegistryKey regLocalMachine = Registry.LocalMachine;
RegistryKey regSYSTEM = regLocalMachine.OpenSubKey("SYSTEM", true);//打开HKEY_LOCAL_MACHINE下的SYSTEM
RegistryKey regControlSet001 = regSYSTEM.OpenSubKey("ControlSet001", true);//打开ControlSet001
RegistryKey regControl = regControlSet001.OpenSubKey("Control", true);//打开Control
RegistryKey regManager = regControl.OpenSubKey("Session Manager", true);//打开Control
RegistryKey regEnvironment = regManager.OpenSubKey("Environment", true);
return regEnvironment;
}
/// <summary>
/// 设置系统环境变量
/// </summary>
/// <param name="name">变量名</param>
/// <param name="strValue">值</param>
public static void SetSysEnvironment(string name, string strValue)
{
OpenSysEnvironment().SetValue(name, strValue);
}
/// <summary>
/// 检测系统环境变量是否存在
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public bool CheckSysEnvironmentExist(string name)
{
if (!string.IsNullOrEmpty(GetSysEnvironmentByName(name)))
return true;
else
return false;
}
/// <summary>
/// 添加到PATH环境变量(会检测路径是否存在,存在就不重复)
/// </summary>
/// <param name="strPath"></param>
public static void SetPathAfter(string strHome)
{
string pathlist ;
pathlist = GetSysEnvironmentByName("PATH");
//检测是否以;结尾
if (pathlist.Substring(pathlist.Length - 1, 1) != ";")
{
SetSysEnvironment("PATH", pathlist + ";");
pathlist = GetSysEnvironmentByName("PATH");
}
string[] list = pathlist.Split(';');
bool isPathExist = false;
foreach (string item in list)
{
if (item == strHome)
isPathExist = true;
}
if (!isPathExist)
{
SetSysEnvironment("PATH", pathlist +strHome+ ";");
}
}
public static void SetPathBefore(string strHome)
{
string pathlist;
pathlist = GetSysEnvironmentByName("PATH");
string[] list = pathlist.Split(';');
bool isPathExist = false;
foreach (string item in list)
{
if (item == strHome)
isPathExist = true;
}
if (!isPathExist)
{
SetSysEnvironment("PATH", strHome + ";" + pathlist);
}
}
public static void SetPath(string strHome)
{
string pathlist;
pathlist = GetSysEnvironmentByName("PATH");
string[] list = pathlist.Split(';');
bool isPathExist = false;
foreach (string item in list)
{
if (item == strHome)
isPathExist = true;
}
if (!isPathExist)
{
SetSysEnvironment("PATH", pathlist + strHome + ";" );
}
}
}
class SetSysEnvironmentVariable
{
[DllImport("Kernel32.DLL ", SetLastError = true)]
public static extern bool SetEnvironmentVariable(string lpName, string lpValue);
public static void SetPath(string pathValue)
{
string pathlist;
pathlist = SysEnvironment.GetSysEnvironmentByName("PATH");
string[] list = pathlist.Split(';');
bool isPathExist = false;
foreach (string item in list)
{
if (item == pathValue)
isPathExist = true;
}
if (!isPathExist)
{
SetEnvironmentVariable("PATH", pathlist + pathValue+";");
}
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
C# 实现设置系统环境变量设置的更多相关文章
- 使用VBScript实现设置系统环境变量的小程序
本人有点桌面洁癖,桌面上只放很少的东西,很多软件都用快捷键调出.最近频繁用到一个软件,我又不想放个快捷方式在桌面,也不想附到开始菜单,于是乎想将其所在目录附加到系统环境变量Path上,以后直接在运行中 ...
- Visual Studio 2012系统环境变量设置(命令行)
方法1.运行脚本vsvars32.bat:D:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\vsvars32.bat ...
- [Java] JDK 系统环境变量设置 bat
@echo off set regpath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environmen ...
- 下载JDK和Jmeter并设置系统环境变量
一.JDK下载并设置系统环境变量 1.JDK官网下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 点左边的 ...
- 通过bat设置系统环境变量
在软件运行过程中,可能需要配置计算机的环境变量,在这里分为两种情况: 一:增加或修改环境变量只在当前软件环境中使用 如我们设置Java的环境变量: set CLASSPATH=%CLASSPATH%; ...
- C# 设置系统环境变量
using Microsoft.Win32; using System; using System.Collections.Generic; using System.ComponentModel; ...
- linux设置系统环境变量的天坑
在设置系统环境变量,也就是 .bash_profile 或者 /etc/proflie 或者 .bashrc 中把path写错或者是把设置系统环境变量的格式写错! 会 导致 系统无法进入.登录无限循环 ...
- DOS永久设置系统环境变量-WMIC
wmic Windows Management Instrumentation Command-line(Windows管理规范命令行) WMIC扩展WMI(Windows Management In ...
- [置顶]
getenv、setenv函数(获取和设置系统环境变量) 与 环境变量
1.getenv() 函数名: getenv 功 能: 从环境中取字符串,获取环境变量的值 头文件: stdlib.h 用 法:char *getenv(char *envvar); 函数说明:get ...
随机推荐
- 在高德地图应用api,和api展出的标记小的应用程序
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- SplashScreenDemo
对Java应用最常见的抱怨就是启动时间太长.这是因为Java虚拟机花费一段时间去加载所有必需的类,特别是对Swing应用,它们需要从Swing和AWT类库代码中去抽取大量的内容. 用户并不喜欢应用程序 ...
- Code Forces 448C Painting Fence 贪婪的递归
略有上升称号,最近有很多问题,弥补啊,各类竞赛滥用,来不及做出了所有的冠军.这个话题 这是一个长期记忆的主题.这是不是太困难,基本技能更灵活的测试,每次我们来看看这个问题可以被删除,处理然后分段层,贪 ...
- ContentMode 几个属性
版权声明:本文博客原创文章,博客,未经同意,不得转载.
- synchronized和进程间通信(转)
关于JAVA多线程同步 JAVA多线程同步主要依赖于若干方法和关键字 1 wait方法: 该方法属于Object的方法,wait方法的作用是使得当前调用wait方法所在部分(代码块)的线程停止执行, ...
- JAVA —— console输入输出
import java.io.*; public class ConsoleIOTest { public static void main(String[] args) { BufferedRead ...
- BI—脚不一样的感觉
在这个网络智能的时代,假设生活和智能挂不上边那就太落后啦!尤其IT行业更是如此,前不久还在用微软的office做报表,这几天就吵吵着换成BI,那么BI是什么?有什么用?怎么用?等等带着这一系列的问题来 ...
- 第三十 访问财富进退自如 —Spring交易管理
6月16日本,明确. "应该留给追穷寇勇,不可沽名学霸王.天若有情天亦老,人间正道是沧桑." 有始有终.有往有还.进退自如乃Spring事务管理之道,也是万物生生不息.和谐共处之道 ...
- DDD Reference
版权声明:本文博客原创文章.博客,未经同意,不得转载.
- HDU 3172 Virtual Friends(并用正确的设置检查)
职务地址:pid=3172">HDU 3172 带权并查集水题.每次合并的时候维护一下权值.注意坑爹的输入. . 代码例如以下: #include <iostream> # ...