gpt4 book ai didi

c# - 如何将 c# 变量从 Windows 窗体应用程序传递给 python 脚本

转载 作者:行者123 更新时间:2023-12-02 04:53:21 24 4
gpt4 key购买 nike

我在用 c# 编写的 Windows 窗体应用程序中通过单击按钮成功调用了 python 脚本,代码如下:

private void login_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("C:\\Python27\\Scripts\\path\\file.py");
}

我现在想将一个变量传递给 python 脚本。我试过将它作为参数传递但无济于事(像这样在 php 中工作):

    private void login_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("C:\\Python27\\Scripts\\path\\file.py myVariable");
}

我在使用此代码的 visual studio 编译器中没有收到任何错误,但是当我单击按钮启动 python 脚本时,我收到一条错误消息,指出“未处理 Win32 异常 - 系统找不到指定的文件”

我也试过这个没有用 - How do I run a Python script from C#?

最佳答案

您需要使用此处所示的开始信息。 http://www.dotnetperls.com/process-start

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\Python27\\Scripts\\path\\file.py";
startInfo.Arguments = "myvariable";

try
{
using (Process exeProcess = Process.Start(startInfo))
{
//dostuff
exeProcess.WaitForExit();
}
}
catch
{
//log
throw;
}

process.start 返回的进程是非托管的,应该在 using 中引用。

关于c# - 如何将 c# 变量从 Windows 窗体应用程序传递给 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18421375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com