3
ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = ;
        start.Arguments = ;  
        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;
        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadToEnd();
                Console.Write(result);
            }
        }

I have this python code to execute python from c#. But i dont know what to put in those two variables.

EDIT

ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = @"U:\Documents\entsoe-py-master\tests\test_data\python.exe";
        start.Arguments = @"U:\Documents\entsoe-py-master\tests\test_data\request.py 31.12.2016 01.01.2017 datatype";  // give filename, dates from the UI to python and query datatype
        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;    
        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadToEnd();
                Console.Write(result);
            }
        }

Im getting this error :

System.ComponentModel.Win32Exception: 'The system cannot find the file specified'

On this line using (Process process = Process.Start(start))

Axel
  • 165
  • 3
  • 14
  • Possible duplication: https://stackoverflow.com/questions/11779143/how-do-i-run-a-python-script-from-c – Prasad Telkikar Jun 05 '18 at 11:25
  • Is it possible that files are not found because of administrators rights? I mean, the paths are good, it shouldnt be the problem – Axel Jun 05 '18 at 11:44
  • Ok, i find the file if i put it in my C: directory. I dont know why it doest find the file which are in my U:. – Axel Jun 05 '18 at 11:54
  • 1
    I don't know about where is your U drive, but we install python in C drive and process runs from C: drive it self. If my answer helps then accept it – Prasad Telkikar Jun 05 '18 at 12:02
  • I finally arrive to execute this file, but i still have errors... – Axel Jun 05 '18 at 12:02
  • what error you are facing now? – Prasad Telkikar Jun 05 '18 at 12:02
  • File "C:\Users\protieax\entsoe-py-master\tests\test_data\request.py", line 6, in import pandas as pd ModuleNotFoundError: No module named 'pandas' It cant import the librairies that are used in my python file. – Axel Jun 05 '18 at 12:05
  • 1
    This error is not related to what you are asking here, this error is related to python, please post another question for it; I would request you to research before posting an question. **I hope issue related to Process.Start or running python file in C# get resolved** – Prasad Telkikar Jun 05 '18 at 12:07

2 Answers2

2

If you don't use the shell, you will have to supply the complete path to the python executable as FileName, and build the Arguments string to supply both your script and the file you want to read.

 ProcessStartInfo start = new ProcessStartInfo();
 start.FileName = cmd;//cmd is full path to python.exe
 start.Arguments = args;//args is path to .py file and any cmd line args
 start.UseShellExecute = false;
 start.RedirectStandardOutput = true;
 using(Process process = Process.Start(start))
 {
     using(StreamReader reader = process.StandardOutput)
     {
         string result = reader.ReadToEnd();
         Console.Write(result);
     }
 }

Hope it helps!

Dungeon
  • 972
  • 2
  • 16
  • 32
  • What is the python executable? Where can i find it? And what is the difference between script and file? – Axel Jun 05 '18 at 11:20
2

To run python code in c#, you need to pass full path of python.exe to FileName and .py file as a argument

ProcessStartInfo start = new ProcessStartInfo();
start.FileName ="Path of python.exe" ;
start.Arguments = "file name ends with .py extension";
start.Verb = "runas";  //To run your process in elevated mode
...remaining code 
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • This is what i tried but it doesnt find the file. I have this error : System.ComponentModel.Win32Exception: 'The system cannot find the file specified' – Axel Jun 05 '18 at 11:21
  • *It doesn't find file*, it means you are passing some wrong path of .py file. Before passing file path as a argument you can check its existance using `File.Exists` method – Prasad Telkikar Jun 05 '18 at 11:23
  • Please update your question with what you have tried so far and what error you are getting – Prasad Telkikar Jun 05 '18 at 11:29
  • I used File.exists and both python.exe and .py file are not found. – Axel Jun 05 '18 at 11:37
  • That means you are passing wrong file paths. If you are windows user, default path of python exe is in C drive i.e. **C:\python3.1\python.exe** not in **Documents** folder and go to file which you want to execute .py files property and copy its path – Prasad Telkikar Jun 05 '18 at 11:41