需要通过nuget安装ironphthon
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using IronPython.Hosting;namespace 转换json
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}private void runTransger(object sender, RoutedEventArgs e){string folderPath = Filedir.Text;pythonconfig pconfig = new pythonconfig();// 定义Python 程序pconfig.pythondir = @"D:\02_Study\miniconda\envs\PyT\python.exe";// 定义Python 运行脚本pconfig.scriptdir = @"D:\05_Trick\Trick\utils_pre.py";pythonThread(folderPath,pconfig);}private void pythonThread(string file,pythonconfig pconfig){string args = $"main_Huaatjsontoxml {file}";// 创建一个新的进程来执行Python脚本Console.WriteLine($"{pconfig.pythondir}, {pconfig.scriptdir} {args}");ProcessStartInfo psi = new ProcessStartInfo(pconfig.pythondir, $"{pconfig.scriptdir} {args}");psi.RedirectStandardOutput = true;psi.UseShellExecute = false;Process process = new Process();process.StartInfo = psi;process.Start();// 获取Python脚本的输出结果string output = process.StandardOutput.ReadToEnd();process.WaitForExit();Console.WriteLine($"结果是: {output}"); //python程序最后return的结果}public class pythonconfig{public string pythondir { get; set; }public string scriptdir { get; set; }}}
}
注意:
[1] python主程序运行最后由os.system(“pause”)的需要注释掉