Recently I was trying to run python script using C# code. However, I encountered some issues beginning until I found a way to do the same.
Here is the C# code to run python script:
ProcessStartInfo startInfo;
Process process;
string directory;
string pyArgs;
string script;
//Python program name here
startInfo = new ProcessStartInfo("python");
startInfo.WorkingDirectory = directory;
//Python script path plus arguments
startInfo.Arguments = script + " " + pyArgs;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
process = new Process();
process.StartInfo = startInfo;
process.Start();
string s;
while ((s = process.StandardOutput.ReadLine()) != null)
{
//do something with s or the output....
}
1 Comments:
What is the value of directory?
Post a Comment
<< Home