2014-03-27 88 views
2

我的wpf应用程序调用python脚本来生成稍后显示在UI中的输出。为避免应用程序崩溃,如果未在用户系统上安装python,我需要执行检查。目前,我已经实现了使用以下WPF检查系统上是否安装了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黑色窗口这是非常不希望的瞬间外观。是否有解决方法来实现此目的或修复以避免出现窗口?

+2

也许add start.CreateNoWindow = true; ? – Dave3of5

+1

可能的复制:http://stackoverflow.com/questions/5377423/hide-console-window-from-process-start-c-sharp – clcto

回答

0

试试这个:或者

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); 
      } 
     } 
5

,你可以检查注册表中的关键HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Python.exe

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

+0

如何检查?我有很多,但徒劳 –

+0

你知道如何以编程方式读取注册表吗? Google“C#读取注册表”。 –

相关问题