Python Forum

Full Version: calling python function with multiple argument in C#
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello All,

I am stuck with a new problem . I have created a simple python code say for summing 2 variables. File is called as Addition.py
Addition.py
import sys
import math
def ad(a,b):
    print(a+b)
Now I am trying to run Additional.py file with a c# application with below mentioned code. I have tried to pass 2 argument for addiing value but still in myString variable i get null value. I do not get any error or any exception while executing code. Wall Wall
public void finalMethod()
        {
            string pyt = @"C:\Python34\python.exe";
            int x = 2;
            int y = 5;
            
            string myPyt = @"D:\Addition.py"; 
            ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(pyt);

            myProcessStartInfo.UseShellExecute = false;
            myProcessStartInfo.RedirectStandardOutput = true;

            myProcessStartInfo.Arguments = myPyt + " " + x + " " + y;
            
            Process myProcess = new Process();
            myProcess.StartInfo = myProcessStartInfo;
            
            myProcess.Start();

            StreamReader myStreamReader = myProcess.StandardOutput;
            string myString = myStreamReader.ReadLine();

            myProcess.WaitForExit();
            myProcess.Close();

            lblText.Content = myString;
        }
My purpose is simple that my main code is written in C# but i have lot of math formulas which i want to write in pyhton and than want to call those function based on process requirement.
Can anyone please help

thanks
As this question requires knowledge of C#, this question is better asked on a C# forum.
However, Someone with C# experience may reply