• C# 环境变量设置


    using Microsoft.Win32;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ZB.QueueSys.Common
    {
        public 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 { 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)
            {
                try
                {
                    OpenSysEnvironment().SetValue(name, strValue);
                }
                catch { return; }
            }
    
            /// <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 = 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 = 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 = 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 + ";");
            }
    
    
        }
    }
    

      

  • 相关阅读:
    Unity3d Shader开发(四)UsePass ,GrabPass ,SubShader Tags
    Unity3d Shader开发(三)Pass(Pass Tags,Name,BindChannels )
    Unity3d Shader开发(三)Pass(Blending )
    Unity3d Shader开发(三)Pass(Alpha testing )
    Unity3d Shader开发(三)Pass(Fog )
    Unity3d Shader开发(三)Pass(Texturing )
    Scrapy 学习笔记爬豆瓣 250
    Python 高效编程技巧实战(2-1)如何在列表,字典, 集合中根据条件筛选数据
    Spring Boot 集成 Spring Security 使用自定义的安全数据源
    SpringBoot 集成 Spring Session
  • 原文地址:https://www.cnblogs.com/YYkun/p/16836944.html
Copyright © 2020-2023  润新知