3

My wpf application calls a python script to generate output which is later displayed in the UI. To avoid crashing of the application if python is not installed on user's system, I need to perform a check. Currently I have achieved that using the following

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

This does the job but causes a momentary appearance of the cmd black window which is highly undesired. Is there a workaround to achieve this or a fix to avoid the appearance of the window?

Pranjal
  • 796
  • 1
  • 8
  • 24

2 Answers2

6

Alternatively, you could check the registry for the default value of the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Python.exe

This is arguably more reliable than just trying to run python.exe, since the Python installer doesn't always update the PATH variable.

Michael Gunter
  • 12,528
  • 1
  • 24
  • 58
1

Try this:

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);
            }
        }
Dave3of5
  • 688
  • 5
  • 23