Mech_Engineer
Mech_Engineer

Reputation: 555

vb.net pass folder path as commandline argument

I'm trying to pass a folder path via commandline argument to an application.

Problem: my folder path contains space " " in it's string. When I read the commandline arguments in the application I get my path chopped into pieces on the space " "

Sub Main()

    Dim arguments As String() = System.Environment.GetCommandLineArgs()
    For Each Arg As String In arguments
        Console.WriteLine("Argument : " & Arg)
    Next

    Console.ReadLine()

End Sub

Edit: added code to build my argument

Private Sub btn_Copy_Click(sender As Object, e As EventArgs) Handles btn_Copy.Click

    Dim args(3) As String
    args(0) = """" & tb_CopyFromPath.Text & """"
    args(1) = """" & tb_CopyToPath.Text & """"
    args(2) = """" & tb_ItemTag.Text & """"
    args(3) = """" & tb_Prefix.Text & """"

    Dim argument As String
    argument = args(0) & " " & args(1) & " " & args(2) & " " & args(3)

    Process.Start("J:\VB.NET - EM AddIn\EM_Design_AddIn\CopyDesign\bin\Debug\CopyDesign.exe", argument)
End Sub

This result isn't okay. The first argument of the first path now contains a piece of the second path.

Screenshot


Edit: add value result from debug.

"""C:\VaultWorkspace\cadcampc\03-Vessel configurator - R2.0\Nozzles\WN_RF_ASME_B16.5\"" ""C:\VaultWorkspace\cadcampc\03-Vessel configurator - R2.0\Test Copy Design\N03"" ""N3"" ""12345-3"""

Upvotes: 1

Views: 3455

Answers (3)

David BS
David BS

Reputation: 1892

Use CHR(34) to delimiter a string with spaces within. See my answer here: vb.net How to pass a string with spaces to the command line

Upvotes: 1

Visual Vincent
Visual Vincent

Reputation: 18310

Just pass it with double-quotes around it.

I.e:

app.exe "C:\Sub folder 1\Sub folder 2"

If you do it in code:

Process.Start("app.exe", """" & path & """")

The quotes specifies the start and end of an argument.


EDIT:

In your case you could do this instead:

argument = """" & args(0) & """  """ & args(1) & """ """ & args(2) & """ """ & args(3) & """"

Upvotes: 1

user2216
user2216

Reputation: 839

It is very simple. Just use ". If you pass test test test parameters, you'll get 3 arguments. But if you write test "test test", you'll receive two parameters: test and test test.

Upvotes: 3

Related Questions