vb.net How to pass a string with spaces to the command line

15,253

Solution 1

It's really an old - but unsolved - problem. My 2 cents of contribution.

Use CHR(34) before-and-after the string, delimiting it like:

Arg = "Name=" & chr(34) & "John Doe da Silva" & chr(34)

Just it!

Solution 2

Please check the below link, its in C#, may be its helpful to you

Word command-line-arguments space issues

Solution 3

Windows does not provide a common way of keeping arguments with spaces as single arguments. However there are a number of relatively common standards that you've tried.

So it comes down to either determining what argument processing mktorrent.exe uses or, as you're trying to pass a filename, using "MSDOS" 8.3 format for the path which will have no spaces.

For the latter, this answer points to the Win32API GetShortPathName.

Of course, 8.3 filenames can be disabled with modern Windows (all Windows NT-based systems I believe -- not that it often is). So your only full solution is to determine what argument processing mktorrent supplies.

Since your comment suggesting the quotes are not being passed through I confirmed I see 'testing' 'testing' '1 2 3' in the MsgBox output of this vbscript:

Option Explicit

Dim arg
Dim line

For Each arg in WScript.Arguments
  line = line & " '" & arg & "'"
Next

MsgBox Trim(line)

when executed using:

Dim strExe As String = "C:\Windows\System32\wscript.exe"
Dim p As New Process
Dim pinfo As New ProcessStartInfo
pinfo.UseShellExecute = False
pinfo.RedirectStandardOutput = True
pinfo.Arguments = " G:\Utils\Arguments.vbs testing ""testing"" ""1 2 3"""
pinfo.FileName = strExe
pinfo.WorkingDirectory = "G:\Utils"
pinfo.WindowStyle = ProcessWindowStyle.Normal
pinfo.CreateNoWindow = True
p.StartInfo = pinfo
p.Start()

So wscript is seeing the quotes and is accumulating three arguments for the script.

BTW I just noticed your example attempts at getting quotes around the filename modify the fn variable. Did you cater for this with the .WorkingDirectory line, which should be using the unmodified filename?

Solution 4

This allows me to pass spaces to cmd. Hours of research turned up nothing; this thread came up constantly, hopefully this will help someone else.

Dim startprgm As New ProcessStartInfo("cmd.exe", "/C """"C:\Program Files (x86)\Folder\File""""" + strArguments)

note that the 4 double quotes lead the path, this part is important. leading the argument (/C) with 5 quotes doesn't work, but the trailing five can be divided into 4 and 1; and structured as such:

Dim startprgm As New ProcessStartInfo("cmd.exe", "/C """"C:\Program Files (x86)""""\Folder\File" + strArguments)

If you open cmd.exe and just send a command, you just need the first quote on the path (it doesn't need to be closed) but VB needs the trailing ones to "close" the quotes out.

best of luck, guys.

Solution 5

This WORKS:

Dim current_path, current_rulename, cmd1 as STRING

current_path = "C:\this folder\file name.exe"    
current_rulename = "file name.exe"

cmd1 = "netsh advfirewall firewall add rule name = """ + current_rulename + """ dir = in action = block program = """ + current_path + """"
cmd1 &= " & "
cmd1 &= "netsh advfirewall firewall add rule name = """ + current_rulename + """ dir = out action = block program = """ + current_path + """"
cmd1 &= " & pause"

Process.Start("cmd", "/c " + cmd1)

Basically, the variables with spaces need to be enclosed like this:

""" + string_with_spaces + """

Broken into parts:

cmd1 = 
"
netsh advfirewall firewall add rule name = 
""" + current_rulename + """
dir=in action=block
program=
""" + current_path + """
"

This code joins two separate commands that use STRINGS with spaces.

Share:
15,253
Chiwda
Author by

Chiwda

Although I never studied Computer Science, I have been programming on and off for 25+ years - since I was an undergrad. My first language was Fortran IV (the output was on punch cards!), then went on to Lisp, VB.Net, C, C++, Python, PHP, JavaScript etc.

Updated on June 04, 2022

Comments

  • Chiwda
    Chiwda about 2 years

    I am trying to call an external program using Process:

        Dim strExe As String = "E:\Projects\Common Files\mktorrent.exe"
        Dim p As New Process
        Dim pinfo As New ProcessStartInfo
        pinfo.UseShellExecute = False
        pinfo.RedirectStandardOutput = True
        pinfo.Arguments = " -a http://blah.com/announce.php -l " & FileSizeMarker & " " & fn
        pinfo.FileName = strExe
        pinfo.WorkingDirectory = fn.Substring(0, fn.LastIndexOf("\"))
        pinfo.WindowStyle = ProcessWindowStyle.Normal
        pinfo.CreateNoWindow = True
        p.StartInfo = pinfo
        p.Start()
    

    The problem is with the filename (variable fn above). If it has spaces, the command chokes - without spaces, it works fine. I have tried adding 1, 2 or3 quotes, like this:

        fn = Chr(34) & Chr(34) & Chr(34) & fn & Chr(34) & Chr(34) & Chr(34)
    

    and also

        fn = "\") & Chr(34) & fn & "\"& Chr(34)
    

    and many other combinations, but it still gives me an error. Any thoughts on how I can get this to work? TIA