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() 
4
  • 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. Commented Mar 14, 2020 at 15:32
  • I fire python and give arguments, I think so Commented Mar 14, 2020 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: stackoverflow.com/questions/3018848/… Commented Mar 14, 2020 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, 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. Commented Mar 16, 2020 at 6:19

1 Answer 1

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\""

Sign up to request clarification or add additional context in comments.

5 Comments

It helps, but now I have permission denied to a file
Are you able to open the file in notepad and save it? Does your application require being run as administrator?
Yes I can open and save file. I run my app from visual studio code I don't know if it's as administrator
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
It doesn't help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.