WPF 检查系统是否安装了 Python
我的WPF应用程序会调用一个Python脚本来生成输出,之后这些输出会显示在用户界面上。为了避免在用户的系统上没有安装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);
}
}
这个方法可以完成任务,但会瞬间出现一个黑色的命令行窗口,这让我很不满意。有没有其他方法可以解决这个问题,或者有什么办法可以避免这个窗口的出现呢?
2 个回答
6
另外,你可以查看注册表中关于 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Python.exe
这个键的默认值。
这样做可能比直接尝试运行 python.exe 更可靠,因为有时候 Python 安装程序并不会更新 PATH
这个变量。
1
试试这个:
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);
}
}