【C#】C# 获取Python安装路径

 private void getPythonLibPath()
        {
            string pathExt = "lib\\site-packages";
            string environment = Environment.GetEnvironmentVariable("Path");
            string [] paths = environment.Split(';');
            string pathWithOutSlash = null;
            foreach (string path in paths)
            {
                bool foundMatch = false;                
                try
                {
                    foundMatch = Regex.IsMatch(path, @"\\Python\d{0,2}\-{0,1}\d{0,2}", RegexOptions.IgnoreCase | RegexOptions.Singleline);
                }
                catch (ArgumentException ex)
                {
                    MessageBox.Show(ex.Message, "异常消息提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                pathWithOutSlash = path.TrimEnd(new char[] { '\\' });

                if (foundMatch)
                {
                    if (File.Exists(path + "python.exe"))
                    {
                        if (Directory.Exists(Path.Combine(pathWithOutSlash, pathExt)))
                        {
                            pythonEnv = pathWithOutSlash;
                            pythonLibPath = Path.Combine(pathWithOutSlash, "lib\\");
                        }  
                    }
                }
            }
        }

猜你喜欢

转载自www.cnblogs.com/haizhibin1989/p/10061642.html