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

python-调用c#代码

环境:
win10,net framework 4,python3.9


镜像:
C#-使用IronPython调用python代码_ironpython wpf-CSDN博客
https://blog.csdn.net/pxy7896/article/details/119929434


目录

  • hello word
    • 不接收参数
    • 接收参数
  • 其他例子

hello word

不接收参数

hello.cs内容如下:

using System;class HelloWorld
{static void Main(){Console.WriteLine("hello world");}
}

python调用:

>>> import subprocess
>>> subprocess.call(['C:/Windows/Microsoft.NET/Framework64/v4.0.30319/csc.exe', 'hello.cs'])
Microsoft (R) Visual C# Compiler version 4.8.4084.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=5332400
>>> result = subprocess.run(['hello.exe'],capture_output=True,text=True)
>>> result.stdout
'Hello, World!\n'

说明:

  1. subprocess.call后,当前路径会产生一个hello.exe,双击没有反应
  2. subprocess.call的第一个参数可能要根据实际情况修改,此处只是我本机的安装路径,仅作参考
  3. 使用subprocess.run才是真正执行,可以反复run

接收参数

修改C#:

using System;class HelloWorld
{static void Main(string[] args){Console.WriteLine(args[0] + " " + args[1]);}
}

python调用的时候就需要带参数:(当然要先call制作新的exe)

>>> result = subprocess.run(['hello.exe', "hello", "world"],capture_output=True,text=True)
>>> result.stdout
'hello world\n'

其他例子

待补充

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

相关文章:

  • 构建铁路安全防线:EasyCVR视频+AI智能分析赋能铁路上道作业高效监管
  • openai command not found (mac)
  • 鸿蒙(API 12 Beta2版)NDK开发【LLDB高性能调试器】调试和性能分析
  • HAL库源码移植与使用之DMA
  • Scrapy爬虫框架介绍、创建Scrapy项目
  • 如何监测某个进程是否退出(C++)?
  • Python:Neo 库读取 ABF 文件,数据格式详解
  • 【Linux】网络基础_3
  • C++之从C过渡(上)
  • MongoDB 100问
  • Arduino ESP32使用 HardwareSerial创建一个任意串口
  • 数据中台建设之数据存储
  • 最常见的AI大模型总结
  • 源码安装docker和docker-compose
  • Java、PHP、Node 操作 MySQL 数据库常用方法
  • nVisual分享社区正式上线啦!
  • 4.5.门控循环单元GRU
  • 10种 Python数据结构,从入门到精通
  • 【AI】人工智能时代,程序员如何保持核心竞争力?
  • WPF学习(3)- WrapPanel控件(瀑布流布局)+DockPanel控件(停靠布局)
  • 【python】Python中实现定时任务常见的几种方式原理分析与应用实战
  • 老公请喝茶,2024年老婆必送老公的养生茶,暖暖的很贴心
  • 3d打印相关资料
  • MySQL1 DDL语言
  • el-tree懒加载状态下实现搜索筛选(纯前端)
  • NLP——Transfromer 架构详解
  • 大模型算法面试题(二十)
  • 2024最新最全面的Selenium 3.0 + Python自动化测试框架
  • 海运中的甩柜是怎么回事❓怎么才能避免❓
  • Win11+docker+gpu+vscode+pytorch配置anomalib(2)