Brandon
Brandon

Reputation: 75

How to use a variable to build Process.Start command?

I'm trying to start an application through CMD using the Process.Start I currently use:

Process.Start("cmd", "/k start C:\Windows\application.exe 127.0.0.1 8484")

However I want to take out the directory and replace it with a variable like so:

Dim line As String = C:\Windows\Application.exe
Process.Start("cmd", "/k start *line* 127.0.0.1 8484")

Upvotes: 0

Views: 1222

Answers (2)

JosefZ
JosefZ

Reputation: 30113

Dim line As String = "C:\Windows\Application.exe"
Process.Start("cmd", "/k start """" """ & line & """ 127.0.0.1 8484")

To stay on the safe side, apply START "title" [/D path] [options] "command" [parameters] syntax pattern:

  • enclose line in an additional pair of double quotes if it contains a blank space character, and
  • definitely use "title":

Always include a title; this can be a simple string like "My Script" or just a pair of empty quotes "".
According to the Microsoft documentation, the title is optional, but depending on the other options chosen you can have problems if it is omitted.

As per MSDN String Data Type (Visual Basic)

Format Requirements
You must enclose a String literal within quotation marks (" "). If you must include a quotation mark as one of the characters in the string, you use two contiguous quotation marks ("")

Upvotes: 4

user4959983
user4959983

Reputation:

Dim line As String = "C:\Windows\Application.exe"
Process.Start("cmd", "/k start " & line & " 127.0.0.1 8484")

According to MSDN ,Process.Start() will accept two string arguments, so it can be used like "/k start " & line & " 127.0.0.1 8484"

Upvotes: 2

Related Questions