C#调用python脚本
C#调用python脚本private Process progressTest;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string path = @"C:\Users\y\Desktop\Python(x,y)2.7.10\Leetcode\CSharpCallPython2.py"; //py文件路径
int a = Convert.ToInt32(this.comboBox1.Text);
int b = Convert.ToInt32(this.comboBox2.Text);
bool trueorfalse = StartTest(path, a, b);
//string path = @"C:\Users\y\Desktop\Python(x,y)2.7.10\Leetcode\CSharpCallPython2.py"; //py文件路径
//string a = Convert.ToString(this.comboBox1.Text);
//string b = Convert.ToString(this.comboBox2.Text);
//Process p = new Process();
////string path = @"C:\Users\y\Desktop\Python(x,y)2.7.10\Leetcode\CSharpCallPython2.py"; //py文件路径
//string sArguments = path;
//ArrayList arrayList = new ArrayList();
//arrayList.Add(a);
//arrayList.Add(b);
//foreach (string param in arrayList)//添加参数
//{
// sArguments += " " + param;
//}
////sArguments += " " + "-u";
//p.StartInfo.FileName = @"D:\ProgramData\Anaconda3\python.exe"; //环境路径需要配置好
//p.StartInfo.Arguments = sArguments; // python命令的参数
//p.StartInfo.UseShellExecute = false;
//p.StartInfo.RedirectStandardOutput = true;
//p.StartInfo.RedirectStandardInput = true;
//p.StartInfo.RedirectStandardError = true;
//p.StartInfo.CreateNoWindow = true;
//p.EnableRaisingEvents = true;
//p.Start();//启动进程
////Console.WriteLine("执行完毕!");
////p.BeginOutputReadLine();
////p.OutputDataReceived += new DataReceivedEventHandler(outputDataReceived);
////Console.ReadKey();
//p.WaitForExit();
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}
}
public bool StartTest(string pathAlg, int a, int b)
{
bool state = true;
if (!File.Exists(pathAlg))
{
throw new Exception("The file was not found.");
return false;
}
string sArguments = pathAlg;
sArguments += " " + a.ToString() + " " + b.ToString();//Python文件的路径用“/”划分比较常见
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"D:\ProgramData\Anaconda3\python.exe"; //环境路径需要配置好
start.Arguments = sArguments;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.RedirectStandardInput = true;
start.RedirectStandardError = true;
start.CreateNoWindow = true;
using (progressTest = Process.Start(start))
{
// 异步获取命令行内容
progressTest.BeginOutputReadLine();
// 为异步获取订阅事件
progressTest.OutputDataReceived += new DataReceivedEventHandler(outputDataReceived);
}
return state;
}
public void outputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
this.Invoke(new Action(() =>
{
this.textBox1.Text = e.Data;
}));
}
}
参考:
【1】c#调用python的方法总结
【2】c#调用python脚本
【3】c#调用python的四种方法
【4】C#调用Python的使用总结
// 方法2
string path = @"C:\Users\y\Desktop\Python(x,y)2.7.10\Leetcode\CSharpCallPython2.py"; //py文件路径
string a = Convert.ToString(this.comboBox1.Text);
string b = Convert.ToString(this.comboBox2.Text);
Process p = new Process();
//string path = @"C:\Users\y\Desktop\Python(x,y)2.7.10\Leetcode\CSharpCallPython2.py"; //py文件路径
string sArguments = path;
ArrayList arrayList = new ArrayList();
arrayList.Add(a);
arrayList.Add(b);
foreach (string param in arrayList)//添加参数
{
sArguments += " " + param;
}
//sArguments += " " + "-u";
p.StartInfo.FileName = @"D:\ProgramData\Anaconda3\python.exe"; //环境路径需要配置好
p.StartInfo.Arguments = sArguments; // python命令的参数
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.EnableRaisingEvents = true;
p.Start(); //启动进程
p.WaitForExit(); import sys
sys.path.append(r"C:\Users\y\Desktop\Python(x,y)2.7.10\Leetcode")
#import os
#import argparse
import numpy
def add(a, b):
return a + b
def mult(a, b):
return a * b
if __name__ == "__main__":
a = float(sys.argv)
b = float(sys.argv)
txt_file = 'result.txt'
with open(txt_file,'a') as f:
f.write(str(a)+'\n')
f.close();
with open(txt_file,'a') as f:
f.write(str(b)+'\n')
f.close();
c1 = add(a, b)
c2 = mult(a, b)
print(c1, c2)
txt_file = 'result.txt'
with open(txt_file,'a') as f:
f.write(str(c1)+'\n')
f.close();
页:
[1]