1. 程式人生 > 其它 >C# 呼叫python子程序並重定向

C# 呼叫python子程序並重定向

 1 /*
 2  * 由SharpDevelop建立。
 3  * 使用者: Administrator
 4  * 日期: 2021/10/15
 5  * 時間: 19:16
 6  * 
 7  * 要改變這種模板請點選 工具|選項|程式碼編寫|編輯標準標頭檔案
 8  */
 9 //主窗體新增 richTextBox1  richTextBox2 Button1 Button2 控制元件,,貼上執行
10 using System;
11 using System.Collections.Generic;
12 using System.Drawing;
13 using System.Windows.Forms;
14 15 using System.Diagnostics; 16 using System.IO; 17 18 namespace SharpCPython_Idle 19 { 20 /// <summary> 21 /// Description of MainForm. 22 /// </summary> 23 public partial class MainForm : Form 24 { 25 System.Diagnostics.Process pyProcess=new Process(); 26 string
pyExePh=@"X:\...\python.exe"; 27 ProcessStartInfo pyInfo; 28 29 StreamWriter pyStmW ; 30 31 public MainForm() 32 { 33 34 InitializeComponent(); 35 System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false
; 36 37 pyInfo=pyProcess.StartInfo; 38 pyInfo.FileName=pyExePh; 39 pyInfo.Arguments="-i"; 40 pyInfo.UseShellExecute=false; 41 pyInfo.CreateNoWindow=true; 42 43 pyInfo.RedirectStandardInput=true; 44 pyInfo.RedirectStandardOutput=true; 45 pyInfo.RedirectStandardError=true; 46 47 pyProcess.OutputDataReceived+= new DataReceivedEventHandler(pyProcess_OutputDataReceived); 48 pyProcess.ErrorDataReceived+= new DataReceivedEventHandler(pyProcess_ErrorDataReceived); 49 50 pyProcess.Start(); 51 52 pyStmW =pyProcess.StandardInput; 53 54 pyProcess.BeginOutputReadLine(); 55 pyProcess.BeginErrorReadLine(); 56 57 58 pyStmW.WriteLine(@"print('\nhello world\n')"); 59 60 61 } 62 63 void pyProcess_OutputDataReceived( Object sender , DataReceivedEventArgs e) 64 { 65 richTextBox2.SelectionColor=Color.Black; 66 richTextBox2.Text+=e.Data; 67 Console.Write( e.Data ); 68 69 } 70 void pyProcess_ErrorDataReceived( Object sender , DataReceivedEventArgs e) 71 { 72 73 Color tmpClr=richTextBox2.SelectionColor; 74 int i=richTextBox2.Text.Length; 75 76 richTextBox2.SelectionColor=Color.Red; 77 richTextBox2.Text+='\n'+e.Data+'\n'; 78 richTextBox2.Select(i,e.Data.Length+1); 79 richTextBox2.DeselectAll(); 80 richTextBox2.SelectionColor=tmpClr; 81 82 } 83 84 void Button1Click(object sender, EventArgs e) 85 { 86 pyStmW.Write(richTextBox1.Text); 87 pyStmW.WriteLine("\n"); 88 89 } 90 91 void Button2Click(object sender, EventArgs e) 92 { 93 richTextBox2.Text=""; 94 } 95 } 96 }

/*
* 由SharpDevelop建立。
* 使用者: Administrator
* 日期: 2021/10/15
* 時間: 19:16
*
* 要改變這種模板請點選 工具|選項|程式碼編寫|編輯標準標頭檔案
*/
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Drawing;
usingSystem.Windows.Forms;

usingSystem.Diagnostics;
usingSystem.IO;

namespaceSharpCPython_Idle
{
///<summary>
///Description of MainForm.
///</summary>
publicpartialclassMainForm : Form
{
System.Diagnostics.Process pyProcess=newProcess();
stringpyExePh=@"D:\Program_Files[LvSe]\blender-2.79b-windows32\blender-2.79b-windows32\2.79\python\bin\python.exe";
ProcessStartInfo pyInfo;

StreamWriter pyStmW;

publicMainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls =false;

pyInfo=pyProcess.StartInfo;
pyInfo.FileName=pyExePh;
pyInfo.Arguments="-i";
pyInfo.UseShellExecute=false;
pyInfo.CreateNoWindow=true;

pyInfo.RedirectStandardInput=true;
pyInfo.RedirectStandardOutput=true;
pyInfo.RedirectStandardError=true;

pyProcess.OutputDataReceived+=newDataReceivedEventHandler(pyProcess_OutputDataReceived);
pyProcess.ErrorDataReceived+=newDataReceivedEventHandler(pyProcess_ErrorDataReceived);

pyProcess.Start();

pyStmW =pyProcess.StandardInput;
//pyStmR=pyProcess.StandardOutput;

pyProcess.BeginOutputReadLine();
pyProcess.BeginErrorReadLine();


pyStmW.WriteLine(@"print('\nhello world\n')");


//TODO: Add constructor code after the InitializeComponent() call.
//
}

voidpyProcess_OutputDataReceived(Object sender,DataReceivedEventArgs e)
{
richTextBox2.SelectionColor=Color.Black;
richTextBox2.Text+=e.Data;
Console.Write(e.Data);

}
voidpyProcess_ErrorDataReceived(Object sender,DataReceivedEventArgs e)
{

Color tmpClr=richTextBox2.SelectionColor;
inti=richTextBox2.Text.Length;

richTextBox2.SelectionColor=Color.Red;
richTextBox2.Text+='\n'+e.Data+'\n';
richTextBox2.Select(i,e.Data.Length+1);
richTextBox2.DeselectAll();
richTextBox2.SelectionColor=tmpClr;

}

voidButton1Click(objectsender,EventArgs e)
{
pyStmW.Write(richTextBox1.Text);
pyStmW.WriteLine("\n");

}

voidButton2Click(objectsender,EventArgs e)
{
richTextBox2.Text="";
}
}
}