Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Diagnostics;
  11. using System.IO;
  12.  
  13. namespace Run_Python_From_C_Sharp
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void button1_Click(object sender, EventArgs e)
  23.         {
  24.             string python = @"C:\Python27\python.exe";
  25.             string myPythonApp = @"C:\Users\Efim\Documents\python\test.py";
  26.  
  27.             int x = (int)numeric_x.Value;
  28.             int y = (int)numeric_y.Value;
  29.  
  30.             ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);
  31.             myProcessStartInfo.UseShellExecute = false;
  32.             myProcessStartInfo.RedirectStandardOutput = true;
  33.  
  34.             myProcessStartInfo.Arguments = myPythonApp;
  35.  
  36.             using (Process myProcess = new Process())
  37.             {
  38.                 myProcess.StartInfo = myProcessStartInfo;
  39.                 lb_status.Text = "Status: Calling Python script with arguments " + x + " and " + y;
  40.                 myProcess.Start();
  41.  
  42.                 using (StreamReader myStreamReader = myProcess.StandardOutput)
  43.                 {
  44.                     string myString = myStreamReader.ReadLine();
  45.                     lb_status.Text = "Status: Value received from script: " + myString;
  46.                     myProcess.WaitForExit();
  47.                 }
  48.  
  49.             }
  50.            
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement