简体   繁体   English

如何将C#表单中的值传递给Python

[英]How can i pass values from a C# form into Python

I am using Visual C# as UI and Python in the background. 我在后台使用Visual C#作为UI和Python。

  1. Enter the details on a visual C# form. 在可视C#表单上输入详细信息。
  2. Clicking a button should run a Python program which should embed the details given in the form into an XML file. 单击按钮应运行一个Python程序,该程序应将表单中提供的详细信息嵌入到XML文件中。
  3. Python should process the XML and ingest into a system. Python应该处理XML并提取到系统中。
  4. Python should monitor for the success from logs and return back the value to be displayed in C# form. Python应该从日志中监视是否成功,并返回要以C#形式显示的值。

Is this possible? 这可能吗?

You could start your python in a new process in the background and pass the form items as arguments in the process start information as if you were running the python script from the command line, like so: 您可以在后台的新进程中启动python,然后将表单项作为参数传递给进程启动信息,就好像您是从命令行运行python脚本一样,如下所示:

var start = new ProcessStartInfo
            {
                FileName = _pathToPythonExecutable,
                Arguments = string.Format(" {0} --arg1 {1}",_pathToYourPythonScript, //formItemValue),
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardInput = true,
                RedirectStandardError = true,
                WorkingDirectory = _currentWorkingDirectory            
            };
using (Process process = Process.Start(start))
            { 
                // Do stuff
            }

You'll see that in my start information, I have told the process to Redirect StandardInput, Standard Output and Standard Error. 您会在开始信息中看到,我已经告诉过程重定向StandardInput,Standard Output和Standard Error。 This is another way that you can pass data between the processes. 这是在进程之间传递数据的另一种方法。

You would write to standard input like so: 您将这样写到标准输入:

process.StandardInput.Write(input);
process.StandardInput.Close();

Standard output and Standard Errors are streams, so you can read them like so 标准输出和标准错误是流,因此您可以像这样读取它们

// This would be the same for standard error
using (StreamReader reader = process.StandardOutput)
                {
                    result = reader.ReadToEnd();
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中同时将datagridview的多个单元格值从一种形式传递到另一种形式? - How can I pass multiple cell values of datagridview from one form to another at same time in C#? 如何在C#中将值从Entity传递到Presentation - How can I pass values from Entity to Presentation in c# 如何将表单值(例如电子邮件,姓名,电话等)从C#控制台应用程序传递到任何网站? - How can I pass form values like email, name, phone etc from my C# console application to any website? C#将值从表格1传递到表格2 - C# Pass values from form 1 to form 2 如何在C#中将多个记录从一种形式传递到另一种形式? - How can I pass multiple records from one form to another form in C#? 如何将值从表单传递到 C# 中的接口类? - How to pass values from a form to an interface class in C#? 如何使用MVC 5 C#将表单中的值传递到电子邮件正文中 - How do I pass the values from my form into an email body using MVC 5 C# 如何通过字典传递表格值 <String, String> 到C#中的对象 - How Do i pass form values from a Dictionary <String, String> to an Object in C# C#从无模形式传递值 - C# Pass Values from Modeless Form 如何从Windows Form应用程序将ac#变量传递给python脚本 - How do I pass a c# variable to a python script from windows form application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM