/// <summary> /// 获取已安装Python版本,存放于PyUtils类的全局静态变量PythonVersions中。 /// </summary> public static void GetPyVersions() { PythonVersions.Clear(); RegistryKey key = Registry.LocalMachine; if (RegUtils.IsItemExists(key, @"SOFTWARE\Python\PythonCore")) { RegistryKey pyVersionKey = key.OpenSubKey(@"SOFTWARE\Python\PythonCore"); string[] pyVersions = pyVersionKey.GetSubKeyNames(); foreach (string version in pyVersions) { string eachPyVarsionPath = @"SOFTWARE\Python\PythonCore\" + version + @"\InstallPath"; if (RegUtils.IsItemExists(key, eachPyVarsionPath)) { RegistryKey eachPyVerKey = key.OpenSubKey(eachPyVarsionPath); string pyPath = eachPyVerKey.GetValue("").ToString(); pyPath = FilePathUtils.RemovePathEndBackslash(pyPath); PyUtils.PythonVersions.Add(version, pyPath); eachPyVerKey.Close(); } } pyVersionKey.Close(); } // 计算冗余值列表 pythonBinaryDuplicatePath.Clear(); foreach (string version in PythonVersions.Keys) { pythonBinaryDuplicatePath.Add(PythonVersions[version]); pythonBinaryDuplicatePath.Add(PythonVersions[version] + "\\Scripts"); } }
/// <summary> /// 把指定值加入到Path环境变量中去 /// </summary> /// <param name="value">指定值(末尾如果有分号或者反斜杠会被去除)</param> /// <param name="append">为true时将值追加到Path变量之后,否则插入到最前</param> public static void AddValueToPath(string value, bool append) { // 先获取Path变量值 List <string> originValues = new List <string>(RegUtils.GetPathVariable(false)); // 处理传入值 value = FilePathUtils.RemovePathEndBackslash(value.Replace("/", "\\")); // 检查重复 if (ListUtils.ListContainsIgnoreCase(originValues, value)) { MessageBox.Show("该路径已经存在于Path变量中!无需再次添加!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } // 开始加入 if (append) { originValues.Add(value); } else { originValues.Insert(0, value); } // 保存Path变量值 if (SavePath(originValues.ToArray())) { MessageBox.Show("已成功添加路径至Path变量!若没有立即生效,请关闭现有已打开终端或者重启电脑再试!", "完成", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("添加路径到Path变量失败!请关闭该程序然后右键-以管理员身份运行该程序再试!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// 返回格式化后的Path变量的副本(将斜杠换成反斜杠并去掉末尾的反斜杠) /// </summary> /// <param name="expand">是否展开其中的变量引用</param> /// <returns>格式化后的Path变量列表</returns> public static string[] GetFormatedPathValues(bool expand) { string[] pathValues = RegUtils.GetPathVariable(expand); for (int i = 0; i < pathValues.Length; i++) { pathValues[i] = FilePathUtils.RemovePathEndBackslash(pathValues[i].Replace("/", "\\")); } return(pathValues); }
/// <summary> /// 运行setx命令设定环境变量 /// </summary> /// <param name="varName">设定变量名</param> /// <param name="value">变量值</param> /// <param name="isSysVar">是否是系统变量</param> public static void RunSetx(string varName, string value, bool isSysVar) { List <string> args = new List <string>(); if (isSysVar) { args.Add("/m"); } args.Add(varName); value = FilePathUtils.RemovePathEndBackslash(value); args.Add(value); TerminalUtils.RunCommand("setx", args.ToArray()); }
/// <summary> /// 检测已安装Adopt OpenJDK版本,信息储存至JDKUtils类的全局静态变量JDKVersions中。 /// </summary> private static void getAdpotJDKVersion() { RegistryKey key = Registry.LocalMachine; // 检测Hotspot VM JDK if (RegUtils.IsItemExists(key, @"SOFTWARE\Temurin\JDK")) { RegistryKey jdkVersionKey = key.OpenSubKey(@"SOFTWARE\Temurin\JDK"); string[] adoptJDKVersions = jdkVersionKey.GetSubKeyNames(); foreach (string adoptJDKVersion in adoptJDKVersions) { RegistryKey infoKey = jdkVersionKey.OpenSubKey(adoptJDKVersion + @"\hotspot\MSI"); string path = infoKey.GetValue("Path").ToString(); path = FilePathUtils.RemovePathEndBackslash(path); JDKVersions.Add(adoptJDKVersion + " - Adopt Hotspot OpenJDK", path); infoKey.Close(); } jdkVersionKey.Close(); } if (RegUtils.IsItemExists(key, @"SOFTWARE\Eclipse Foundation\JDK")) { RegistryKey jdkVersionKey = key.OpenSubKey(@"SOFTWARE\Eclipse Foundation\JDK"); string[] adoptJDKVersions = jdkVersionKey.GetSubKeyNames(); foreach (string adoptJDKVersion in adoptJDKVersions) { RegistryKey infoKey = jdkVersionKey.OpenSubKey(adoptJDKVersion + @"\hotspot\MSI"); string path = infoKey.GetValue("Path").ToString(); path = FilePathUtils.RemovePathEndBackslash(path); JDKVersions.Add(adoptJDKVersion + " - Adopt Hotspot OpenJDK", path); infoKey.Close(); } jdkVersionKey.Close(); } // 检测OpenJ9 VM JDK if (RegUtils.IsItemExists(key, @"SOFTWARE\Semeru\JDK")) { RegistryKey jdkVersionKey = key.OpenSubKey(@"SOFTWARE\Semeru\JDK"); string[] adoptJDKVersions = jdkVersionKey.GetSubKeyNames(); foreach (string adoptJDKVersion in adoptJDKVersions) { RegistryKey infoKey = jdkVersionKey.OpenSubKey(adoptJDKVersion + @"\openj9\MSI"); string path = infoKey.GetValue("Path").ToString(); path = FilePathUtils.RemovePathEndBackslash(path); JDKVersions.Add(adoptJDKVersion + " - Adopt OpenJ9 OpenJDK", path); infoKey.Close(); } jdkVersionKey.Close(); } key.Close(); }
/// <summary> /// 检测已安装Azul Zulu OpenJDK版本,信息储存至JDKUtils类的全局静态变量JDKVersions中。 /// </summary> private static void getAzulZuluJDKVersion() { RegistryKey key = Registry.LocalMachine; if (RegUtils.IsItemExists(key, @"SOFTWARE\Azul Systems\Zulu")) { RegistryKey jdkVersionKey = key.OpenSubKey(@"SOFTWARE\Azul Systems\Zulu"); string[] zuluJDKVersions = jdkVersionKey.GetSubKeyNames(); foreach (string zuluJDKVersion in zuluJDKVersions) { RegistryKey infoKey = jdkVersionKey.OpenSubKey(zuluJDKVersion); string path = infoKey.GetValue("InstallationPath").ToString(); path = FilePathUtils.RemovePathEndBackslash(path); JDKVersions.Add(zuluJDKVersion + " - Azul Zulu OpenJDK", path); infoKey.Close(); } jdkVersionKey.Close(); } }
/// <summary> /// 检测已安装Microsoft JDK版本,信息储存至JDKUtils类的全局静态变量JDKVersions中。 /// </summary> private static void getMicrosoftJDKVersion() { RegistryKey key = Registry.LocalMachine; if (RegUtils.IsItemExists(key, @"SOFTWARE\Microsoft\JDK")) { RegistryKey msJDKVersionKey = key.OpenSubKey(@"SOFTWARE\Microsoft\JDK"); string[] msJDKVersions = msJDKVersionKey.GetSubKeyNames(); foreach (string msJDKVersion in msJDKVersions) { RegistryKey jdkInfoKey = msJDKVersionKey.OpenSubKey(msJDKVersion + @"\hotspot\MSI"); string path = jdkInfoKey.GetValue("Path").ToString(); path = FilePathUtils.RemovePathEndBackslash(path); JDKVersions.Add(msJDKVersion + " - Microsoft Build OpenJDK", path); jdkInfoKey.Close(); } msJDKVersionKey.Close(); } key.Close(); }
/// <summary> /// 从Path环境变量中移除不存在的路径 /// </summary> /// <returns>返回移除了不存在的路径后的Path变量</returns> public static string[] RemoveNotExistPathInPathValues() { List <string> result = new List <string>(RegUtils.GetPathVariable(false)); Dictionary <string, string> variables = GetVariablesInPath(); string eachPath; for (int i = 0; i < result.Count; i++) { // 如果这个值中包含%,说明是变量引用形式,获取其实际值 if (result[i].Contains("%")) { // 变量列表中不存在该值,说明这个值是不存在的路径 eachPath = FilePathUtils.RemovePathEndBackslash(result[i].Replace("/", "\\")); if (!variables.ContainsKey(eachPath)) { result.RemoveAt(i); i--; continue; } eachPath = FilePathUtils.RemovePathEndBackslash(variables[eachPath].Replace("/", "\\")); // 否则,进一步检测其对应路径是否存在 if (!Directory.Exists(eachPath) && !File.Exists(eachPath)) { result.RemoveAt(i); i--; } } else { // 否则就是普通路径,检测是否存在即可 eachPath = FilePathUtils.RemovePathEndBackslash(result[i].Replace("/", "\\")); if (!Directory.Exists(eachPath) && !File.Exists(eachPath)) { result.RemoveAt(i); i--; } } } return(result.ToArray()); }
/// <summary> /// 检测已安装Oracle JDK版本,信息储存至JDKUtils类的全局静态变量JDKVersions中。 /// </summary> private static void getOracleJDKVersion() { RegistryKey key = Registry.LocalMachine; //检测jdk8及其以下版本 if (RegUtils.IsItemExists(key, @"SOFTWARE\JavaSoft\Java Development Kit")) { RegistryKey jdkOldVersionsKey = key.OpenSubKey(@"SOFTWARE\JavaSoft\Java Development Kit"); string[] jdkOldVersions = jdkOldVersionsKey.GetSubKeyNames(); foreach (string version in jdkOldVersions) { if (Array.IndexOf(NOT_ADD_VERSION_VALUE, version) == -1) { RegistryKey jdkVersionKey = key.OpenSubKey(@"SOFTWARE\JavaSoft\Java Development Kit\" + version); string path = jdkVersionKey.GetValue("JavaHome").ToString(); path = FilePathUtils.RemovePathEndBackslash(path); JDKVersions.Add(version + " - Oracle JDK", path); jdkVersionKey.Close(); } } jdkOldVersionsKey.Close(); } //检测jdk9及其以上版本 if (RegUtils.IsItemExists(key, @"SOFTWARE\JavaSoft\JDK")) { RegistryKey jdkNewVersionsKey = key.OpenSubKey(@"SOFTWARE\JavaSoft\JDK"); string[] jdkNewVersions = jdkNewVersionsKey.GetSubKeyNames(); foreach (string version in jdkNewVersions) { RegistryKey jdkVersionKey = key.OpenSubKey(@"SOFTWARE\JavaSoft\JDK\" + version); string path = jdkVersionKey.GetValue("JavaHome").ToString(); path = FilePathUtils.RemovePathEndBackslash(path); JDKVersions.Add(version + " - Oracle JDK", path); jdkVersionKey.Close(); } jdkNewVersionsKey.Close(); } }