当前位置: 首页 > news >正文

C#调用python 程序

需要通过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”)的需要注释掉

http://www.lryc.cn/news/501701.html

相关文章:

  • day11 性能测试(4)——Jmeter使用(黑马的完结,课程不全)
  • 机器学习详解(4):多层感知机MLP之理论学习
  • 【C++】类中的特殊成员——静态成员,友元成员,常量成员
  • 开源 Agent 小屋
  • Mina之账户模型
  • STM32 ADC 配置
  • 练9:进制转换
  • 善于运用指针--函数与指针
  • Microi吾码低代码平台:前端源码的本地运行探索
  • 十一、容器化 vs 虚拟化-Docker 使用
  • 实践项目2-自动计价电子秤
  • iOS如何操作更新推送证书
  • WSL2 在vscode无法连接copilot
  • HTA8998 实时音频跟踪的高效内置升压2x10W免电感立体声ABID类音频功放
  • 用ChatGPT-o1进行论文内容润色效果怎么样?
  • 《探索 Jetpack Compose:构建现代化 Android UI 的利器》
  • cocos creator 的 widget组件的使用及踩坑
  • Baumer工业相机的EMVA1288 数据报告简介
  • Docker 安装 中文版 GitLab
  • uni-app 个人课程表页面
  • FPGA工作原理、架构及底层资源
  • 【OpenCV】平滑图像
  • LeetCode300. 最长递增子序列(2024冬季每日一题 30)
  • vue H5如何实现copy功能
  • Golang使用etcd构建分布式锁案例
  • Windows 和 Ubuntu 双系统安装
  • 多媒体文件解复用(Demuxing)过程
  • 从 Zuul 迁移到 Spring Cloud Gateway:一步步实现服务网关的升级
  • qt之插件编译
  • pandas一行拆成多行