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?

2

2 Answers 2

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.

3
  • how can it be checked? I have a lot but in vain Commented Oct 19, 2017 at 15:32
  • Do you know how to read the registry programmatically? Google "C# read registry". Commented Oct 23, 2017 at 17:51
  • 1
    I have Python Installed and running on my machine but I don't have it's Entry in App paths. But I can see the Python's Entry under Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Python Commented Aug 26, 2019 at 10:41
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);
            }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.