0

I want to display standard output from python script in textbox, but after click the button nothing happend. If it's wrong how can I fix it or replace? Example I run my aplication in textbox1 i have nothing and after clicking button1 I want to have in my textbox1 'Hello' from python script

private void button1_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
            {
                char[] spliter = { '\r' };
                int x = 1;

                Process python = new Process();
                python.StartInfo.FileName = "D:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python37_64\\python.exe";
                python.StartInfo.RedirectStandardOutput = true;
                python.StartInfo.UseShellExecute = false;

                python.StartInfo.Arguments = string.Concat("C:\\Users\\kamil\\source\\PythonApplication3\\PythonApplication3.py", " ", x.ToString());
                python.Start();

                StreamReader sReader = python.StandardOutput;
                string[] output = sReader.ReadToEnd().Split(spliter);


                foreach (string s in output)
                {
                    TextBox1.Text += s + Environment.NewLine;
                }

                python.WaitForExit();

Python script:

import sys

def main():
    print('Hello')

main()
Kamil
  • 25
  • 6
  • It's the shell that knows how to interpret file names ending in *".py"* as python programs. You've turned ShellExecute off, so that doesn't happen. You'll need to fire up python and then pass it your script and arguments somehow. – Flydog57 Mar 14 '20 at 15:32
  • I fire python and give arguments, I think so – Kamil Mar 14 '20 at 15:51
  • Hi. Can you check this post? The link in the answer is broken, but I think the post can still be much of a help: https://stackoverflow.com/questions/3018848/cannot-redirect-output-when-i-run-python-script-on-windows-using-just-scripts-n – Oguz Ozgul Mar 14 '20 at 16:11
  • The UWP is running in the sandbox and is different from the desktop app, you can launch your exe file by using [FullTrustProcessLauncher](https://learn.microsoft.com/en-us/uwp/api/Windows.ApplicationModel.FullTrustProcessLauncher), but can't access the output from a UWP application. Can you tell us why you want to do this? In addition, you can create a wpf application to use Process api and then convert the wpf application to UWP. – Faywang - MSFT Mar 16 '20 at 06:19

1 Answers1

0

Try adding python.StartInfo.RedirectStandardError = true; and textBox1.Text += python.StandardError.ReadToEnd(); to your code so that you can also capture the errors in addition to the output. I would reason that it may not be able to "find" the file you are passing as an argument. Try adding quotes around the file path like so: "\"C:\Users\kamil\source\PythonApplication3\PythonApplication3.py\""

Rob
  • 628
  • 5
  • 9
  • It helps, but now I have permission denied to a file – Kamil Mar 14 '20 at 17:07
  • Are you able to open the file in notepad and save it? Does your application require being run as administrator? – Rob Mar 14 '20 at 17:09
  • Yes I can open and save file. I run my app from visual studio code I don't know if it's as administrator – Kamil Mar 14 '20 at 17:14
  • Try closing visual studio, right clicking on the visual studio shortcut, and choosing "Run as administrator" or going to your debug folder and running the outputted exe as administrator – Rob Mar 14 '20 at 17:17
  • It doesn't help – Kamil Mar 14 '20 at 17:24