c# - 从 C# (.NET) 调用 Python 函数

标签 c# python .net

我有 Visual Studio 2015,我的主窗体是用 C# 编写的,从那里我有不同的类是用 Python 编写的(普通 Python 不是 Iron Python)。如何从我的 C# 代码中调用 Python 函数?

我知道有多个关于此的主题,但其中大多数都太老了,有些解决方案太难或涉及使用像 C++ 这样的中间语言。

以下是一些我觉得有用但没有提供我正在搜索的答案的链接:

是否有简单的方法或者我仍然需要解决方法?如果我需要解决方法,那么最简单的方法是什么?

最佳答案

你可以通过你的命令行调用一切

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "python whatever";
process.StartInfo = startInfo;
process.Start();

或者更好的是,只需调用 Python.exe 并将您的 py 文件作为参数传递:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "Python.exe";
startInfo.Arguments = "yourfile.py";
process.StartInfo = startInfo;
process.Start();

关于c# - 从 C# (.NET) 调用 Python 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35462175/

相关文章:

javascript - 在MVC 4.0母版页中加载jquery

c# - 如何将 Web API 以外的所有内容路由到/index.html

python - "ImportError: No module named serial"- 安装 pyserial 之后

python - PyYAML 给出构造函数错误?

python - 在具有单独图例的一个图中复制 Python 中的相同图

.net - 是否可以在Elasticsearch路径中使用lambda表达式?

c# - 单击一次更新到特定版本

c# - 如何在C#中实现单例?

.net - 如何在vb.net中对结构数组的相似元素求和?

c# - C#-程序添加一个额外的'找到'的地方