【问题标题】:WPF Check if python installed on systemWPF 检查系统上是否安装了 python
【发布时间】:2014-05-06 19:19:11
【问题描述】:

我的 wpf 应用程序调用 python 脚本来生成输出,该输出稍后会显示在 UI 中。如果用户的系统上没有安装 python,为了避免应用程序崩溃,我需要进行检查。目前我已经使用以下方法实现了这一目标

ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"cmd.exe"; // Specify exe name.
start.Arguments = "python --version";
start.UseShellExecute = false;
start.RedirectStandardError = true;

using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardError)
            {
                string result = reader.ReadToEnd();
                MessageBox.Show(result);
            }
        }

这可以完成工作,但会导致 cmd 黑色窗口暂时出现,这是非常不希望的。是否有解决方法来实现此目的或修复以避免出现窗口?

【问题讨论】:

标签: c# python wpf cmd


【解决方案1】:

或者,您可以检查注册表中键 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Python.exe 的默认值

这可以说比仅仅尝试运行 python.exe 更可靠,因为 Python 安装程序并不总是更新 PATH 变量。

【讨论】:

  • 如何检查?我有很多,但都是徒劳的
  • 你知道如何以编程方式读取注册表吗?谷歌“C# 读取注册表”。
  • 我已经在我的机器上安装并运行了 Python,但我没有它的应用程序路径中的条目。但我可以在 Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Python 下看到 Python 的条目
【解决方案2】:

试试这个:

ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"cmd.exe"; // Specify exe name.
start.Arguments = "python --version";
start.UseShellExecute = false;
start.RedirectStandardError = true;
start.CreateNoWindow = true;

using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardError)
            {
                string result = reader.ReadToEnd();
                MessageBox.Show(result);
            }
        }

【讨论】:

    猜你喜欢
    • 2020-10-27
    • 2014-10-26
    • 2021-11-23
    • 1970-01-01
    • 2010-09-11
    • 1970-01-01
    • 2021-01-04
    • 2010-11-06
    • 2015-10-14
    相关资源
    最近更新 更多