C# 实现设置系统环境变量设置

系统 1954 0
原文: C# 实现设置系统环境变量设置

以前实现系统环境变量设置时是要在电脑属性--高级--环境变量设置,实现方式主要有2种,

  1. 修改注册表,添加环境变量
  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 + ";" );

}

}


}
 
 
Kernel32.DLL内有SetEnvironmentVariable函数用于设置系统环境变量
C#调用要用DllImport,代码封装如下:
        
          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+ ";" );

}

}
}

 

C# 实现设置系统环境变量设置


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论