I have to launch Python from another folder. To do that before I have to change folder. Just like if I did Cd ..pythonPath... The tasks I do are:
Get Python path from the enviroment variable
string strPath = Environment.GetEnvironmentVariable("Path"); string[] splitPath = strPath.Split(';'); string strPythonPath = String.Empty; foreach (string path in splitPath) { if (path.ToUpper().Contains("PYTHON")) { strPythonPath = path; break; } }
With that I get the script folder so I move one up
strPythonPath = Path.GetFullPath( Path.Combine(strPythonPath, ".."));
I launch the external process
Process ExternalProcess = new Process(); ExternalProcess.StartInfo.FileName = "Phyton.exe"; string strPythonScript = @"C:\temp\script.py"; string strTestPool = "testPool.xml"; ExternalProcess.StartInfo.WorkingDirectory = strPythonPath; ExternalProcess.StartInfo.Arguments = strPythonScript + " " + strTestPool + " " + strTemp; ExternalProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized; ExternalProcess.Start(); ExternalProcess.WaitForExit();
With that I get unable to find the specified file. Of course I might ALSO puth that complete path in
ExternalProcess.StartInfo.FileName = " C:\Program Files\Python39\Phyton.exe";
but is that the right thing to do?
Once again what I would like is to move beforehand just like doing
cd C:\Program Files\Python39
and additionally could it be Directory.SetCurrentDirectory(...) the solution?
Thanks