Hack Forums
[C#] start python application from c# code - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] start python application from c# code (/showthread.php?tid=6086574)



start python application from c# code - madeha elsayedxxxx - Jun 11, 2020

hello every one
i am new in c# , i made python application and it compiled correctly ,
however i want to start this python application from c# code using(Process.Start("");)
but it doesn't work!
how to run this python application from c# code
please not that the application is compiled ,
i want to run the application (not the code)


RE: start python application from c# code - Shadow_Cloud - Jun 11, 2020

So maybe show us some code and an error? Process.Start should work?


RE: start python application from c# code - SaketSaket - Jun 11, 2020

Is this useful for you...?

Quote:public string run_cmd(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "PATH_TO_PYTHON_EXE";
start.Arguments = string.Format("\"{0}\" \"{1}\"", cmd, args);
start.UseShellExecute = false;// Do not use OS shell
start.CreateNoWindow = true; // We don't need new window
start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back
start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script
string result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test")
return result;
}
}
}




RE: start python application from c# code - madeha elsayedxxxx - Jun 11, 2020

(Jun 11, 2020 09:06 AM)Shadow_Cloud Wrote:  So maybe show us some code and an _error_?  Process.Start should work?
there wasnot any error
if i double click to python application , it runs correctly
if i use process.start(c# code) it dosont run


RE: start python application from c# code - FrenchTech - Jun 11, 2020

Try this code:
Quote:       string fileName = @"C:\your_script_name.py";

           Process p = new Process();
           p.StartInfo = new ProcessStartInfo(@"C:\Python27\python.exe", fileName) // full path to python path + script
           {
               RedirectStandardOutput = true,
               UseShellExecute = false,
               CreateNoWindow = true
           };
           p.Start();

           string output = p.StandardOutput.ReadToEnd();
           p.WaitForExit();

           Console.WriteLine(output);




RE: start python application from c# code - Accord - Jun 11, 2020

Does your python already in executable format or still in .py extension??


RE: start python application from c# code - madeha elsayedxxxx - Jun 12, 2020

(Jun 11, 2020 04:55 PM)Accord Wrote:  Does your python already in executable format or still in .py extension??
in executable format


RE: start python application from c# code - XaXBioX - Jun 16, 2020

make new process and add your python application agreements