1

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();
  }
}
4
  • What do you mean by "doesn't work"? What actually goes wrong?
    – Katriel
    Commented Oct 14, 2012 at 13:58
  • the script dosn't run at all it should be generating something in my Report folders but nothing happens. If i do it manually it works and generates something in the Report folder.
    – rexer
    Commented Oct 14, 2012 at 13:59
  • Do you see any error messages in your Output window of Visual Studio when it runs?
    – Joe L.
    Commented Oct 14, 2012 at 14:06
  • nope no errors at all only these System.Diagnostics.ProcessStartInfo System.Diagnostics.ProcessStartInfo System.Diagnostics.ProcessStartInfo System.Diagnostics.ProcessStartInfo
    – rexer
    Commented Oct 14, 2012 at 14:08

1 Answer 1

0

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

3
  • I added it into my %PATH%, like i said when i do it manually via cmd it works when typing "python"
    – rexer
    Commented Oct 14, 2012 at 14:04
  • But is it in you path when you launch the C# program?
    – cdarke
    Commented Oct 14, 2012 at 15:00
  • Yes it is in the path of my C# program in bin/Debug
    – rexer
    Commented Oct 14, 2012 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.