Running a python script on C# not working

2019-08-15 10:18发布

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();
  }
}

标签: c# python cmd
1条回答
虎瘦雄心在
2楼-- · 2019-08-15 10:53

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

查看更多
登录 后发表回答