• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 673
  • Last Modified:

Run batch file with arguments from Visual basic .Net

I am trying to run a batch fine from Visual basic .Net. I have tried this:
Dim runString As String = ControlChars.Quote & Application.StartupPath & "\myBat.bat" & ControlChars.Quote
System.Diagnostics.Process.Start(runString & " " & ControlChars.Quote & lstServersBox.GetItemText(lstServersBox.Items(intServerCounter)) & ControlChars.Quote & " " & ControlChars.Quote & txtUsername.Text & ControlChars.Quote & " " & ControlChars.Quote & txtPassword.Text & ControlChars.Quote & " " & ControlChars.Quote & sPath & ControlChars.Quote)

Also this:
Dim runString As String = ControlChars.Quote & Application.StartupPath & "\myBat.bat" & ControlChars.Quote
Shell("cmd /c " & runString & " " & """" & lstServersBox.GetItemText(lstServersBox.Items(intServerCounter)) & """" & " " & """" & txtUsername.Text & """" & " " & """" & txtPassword.Text & """" & " " & """" & sPath & """", AppWinStyle.Hide)

In the first example it seems to try to execute the whole thing as one path and not one path with arguments. In the second example, no matter how I tru to quote the string, it doesn't find the path if there is a space in it, or if there is none, tries to execute the whole thing as if it was one path and not a path+filename+arguments.

Any tips?
0
itnifl
Asked:
itnifl
1 Solution
 
käµfm³d 👽Commented:
For the .NET version, you send the parameter string as the second parameter to Process.Start()--not concatenated to the program name.

Try this (it should be easier to read):
Dim runString As String = String.Format("""{0}\{1}""", Application.StartupPath, "myBat.bat")
Dim parameters As String = String.Format("""{0}"" ""{1}"" ""{2}"" ""{3}""", lstServersBox.GetItemText(lstServersBox.Items(intServerCounter)), txtUsername.Text, txtPassword.Text, sPath)

System.Diagnostics.Process.Start(runString, parameters)

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now