Esempio n. 1
0
        /// <summary>
        /// 设定Python环境变量
        /// </summary>
        /// <param name="pyPath">python所在位置</param>
        public static void SetPythonValue(string pyPath)
        {
            // 先设定PYTHON_HOME变量
            VariableUtils.RunSetx(PYHOME_NAME, pyPath, true);
            if (!RegUtils.IsValueExists(Registry.LocalMachine, @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", PYHOME_NAME))
            {
                MessageBox.Show("设定PYTHON_HOME失败!请退出程序然后右键-以管理员身份运行此程序重试!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            // 再设定Path变量
            // 先执行Path去重
            List <string> pathValues = new List <string>(PathValuesUtils.RemoveDuplicateValueInPathAndFormat());

            // 去除冗余路径
            ListUtils.BatchRemoveFromList(pathValues, pythonBinaryDuplicatePath);
            // 加入到Path
            if (!ListUtils.ListContainsIgnoreCase(pathValues, PATH_ADDITION_1))
            {
                pathValues.Add(PATH_ADDITION_1);
            }
            if (!ListUtils.ListContainsIgnoreCase(pathValues, PATH_ADDITION_2))
            {
                pathValues.Add(PATH_ADDITION_2);
            }
            // 保存Path
            if (!VariableUtils.SavePath(pathValues.ToArray()))
            {
                MessageBox.Show("追加Path失败!请退出程序然后右键-以管理员身份运行此程序重试!也可能是Path变量总长度超出限制!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            MessageBox.Show("设置完成!", "完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Esempio n. 2
0
        /// <summary>
        /// 设定JDK环境变量
        /// </summary>
        /// <param name="javaPath">jdk所在的路径</param>
        /// <param name="isJDK9AndAbove">是否是jdk9及其以上版本的jdk</param>
        public static void SetJDKValue(string javaPath, bool isJDK9AndAbove)
        {
            RegistryKey key   = Registry.LocalMachine;
            RegistryKey EVKey = key.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true);

            // 先设定JAVA_HOME变量
            VariableUtils.RunSetx("JAVA_HOME", javaPath, true);
            if (!RegUtils.IsValueExists(key, @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "JAVA_HOME"))
            {
                MessageBox.Show("设定JAVA_HOME失败!请退出程序然后右键-以管理员身份运行此程序重试!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            // 再设定classpath变量
            if (isJDK9AndAbove)
            {
                if (RegUtils.IsValueExists(key, @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "classpath"))
                {
                    EVKey.DeleteValue("classpath");
                }
            }
            else
            {
                if (!RegUtils.IsValueExists(key, @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "classpath") || !EVKey.GetValue("classpath").Equals(CLASSPATH_VALUE))
                {
                    VariableUtils.RunSetx("classpath", CLASSPATH_VALUE, true);
                }
            }
            EVKey.Close();
            key.Close();
            if (!isJDK9AndAbove && !RegUtils.IsValueExists(key, @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "classpath"))
            {
                MessageBox.Show("设定classpath失败!请退出程序然后右键-以管理员身份运行此程序重试!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            // 最后设定Path变量
            // 执行Path去重
            List <string> pathValues = new List <string>(PathValuesUtils.RemoveDuplicateValueInPathAndFormat());

            // 去除冗余的Java bin路径
            ListUtils.BatchRemoveFromList(pathValues, javaBinaryDuplicatePath);
            // 添加到Path
            if (!ListUtils.ListContainsIgnoreCase(pathValues, ADD_PATH_VALUE))
            {
                pathValues.Add(ADD_PATH_VALUE);
            }
            // 保存Path
            if (!VariableUtils.SavePath(pathValues.ToArray()))
            {
                MessageBox.Show("追加Path值失败!请退出程序然后右键-以管理员身份运行此程序重试!也可能是Path变量总长度超出限制!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            MessageBox.Show("设置完成!", "完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }