如何从C#运行Python脚本?
if __name__ == '__main__': with open(sys.argv[1], 'r') as f: s = f.read() print s
private void run_cmd(string cmd, string args) { ProcessStartInfo start = new ProcessStartInfo(); start.FileName = cmd; start.Arguments = 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); } } }
code.py
cmd
filename
args
python.exe
cmd
code.py filename
args
.
private void get_vals() { for (int i = 0; i < 100; i++) { run_cmd("code.py", i); } }
相关分类