简体   繁体   中英

How to integrate python code with C# in Visual Studio 2019

Python code, which is executed on Google Colab and downloaded in.py file how to integrate it with C# code in Visual Studio 2019. On button click, Python code should execute in background and output should be displayed on WPF application.

Python code can be executed in c# by IronPython library and via command prompt . In IronPython I feel some issues. So, I will suggest you to make a process and execute it by cmd. Here is the code to do so.

 ```Process proc = new Process();
            ProcessStartInfo info = new ProcessStartInfo();
            info.WindowStyle = ProcessWindowStyle.Normal;
            info.FileName = "cmd.exe";
            info.Arguments = "/C python code.py \""+arguments+"\""; 
            info.RedirectStandardOutput = true;
            info.UseShellExecute = false;
            info.CreateNoWindow = false;
            proc.StartInfo = info;
            proc.Start();
            proc.WaitForExit();
            string line = "";
            while (!proc.StandardOutput.EndOfStream)
            {
                line += proc.StandardOutput.ReadLine();

            }```

But you have to install all the libraries in PC first which must be overall in the PC not only in some folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM