Running a python script on C# not working

2019-08-15 10:41发布

问题:

I have this code that I'm trying to run a Python script. It works correctly when I do it manually via the command prompt but if I try to do it via a button click in a C# Windows form it doesn't work.

private void btnGenerateAndrowarn_Click(object sender, EventArgs e) {
  string[] filePaths = Directory.GetFiles(@"C:\Users\User1\Desktop\Android Tools\androwarn\Samples", "*.apk");
  foreach (string fileName in filePaths) {
    ProcessStartInfo startInfo;
    Process process;
    string directory = @"C:\Users\User1\Desktop\Android Tools\androwarn\";
    string script = "androwarn.py";
    startInfo = new ProcessStartInfo("python");
    startInfo.WorkingDirectory = directory;
    startInfo.Arguments = directory + script + " -i " + fileName + " -r html -v 3";
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;
    process = new Process();
    process.StartInfo = startInfo;
    process.Start();
  }
}

回答1:

Most likely is that python is not in your %PATH%.



标签: c# python cmd