3

I want to run a Python script in cmd when a button is clicked in VB.Net. Do you need to call the Python script in a batch file? Or can you do it directly in VB?

I tried this

Dim ps As New ProcessStartInfo("cmd.exe")
    ps.Arguments = "C:\yourbatfile.bat"
    Process.Start(ps)

But that only opens the cmd window and doesn't execute the bat file.

1
  • I think the argument you sent is incorrect (usually you'd suffix it with /c or /k when running cmd), but why open cmd when you can just execute the batch file immediately? Process.Start("C:\yourbatfile.bat") Commented Aug 8, 2016 at 10:01

1 Answer 1

2
Dim psi = New ProcessStartInfo("c:\python27\python.exe", "myPythonScript.py")
Dim proc = Process.Start(psi)
proc.WaitForExit()
If proc.ExitCode <> 0 then throw new Exception(" Script Failed")
Sign up to request clarification or add additional context in comments.

2 Comments

I get this error with that code: 'System.Diagnostics.Assert' is not accessible in this context because it is 'Friend'.
That was just to show you how to test the exit code to see if errors had occurred. I've changed it to throw an exception now.

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.