Skip to content

Instantly share code, notes, and snippets.

@0x49D1
Last active February 6, 2023 09:04
Show Gist options
  • Save 0x49D1/048f6d3ce1d7345b4464fd81e701f310 to your computer and use it in GitHub Desktop.
Save 0x49D1/048f6d3ce1d7345b4464fd81e701f310 to your computer and use it in GitHub Desktop.
Run python script in initialized process
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment