Command Line Arguments

Ford-p

Active member
Joined
Mar 1, 2007
Messages
30
Programming Experience
Beginner
Hey,

I'm trying to get the following command line arguments:
VB.NET:
-save c:\my file.txt -print -close

The single switches aren't a problem but i can't find a way of making it accept a location, especially if it has a space.

I did search but couldn't find anything useful, I'm surprised this isn't a more common question.

Thanks,
Joe
 
Environment.GetCommandLineArgs()
 
Thanks for your reply, but I don't think you understand my problem fully.

I know how to get the simple arguments, but I can't work out how to get file names with spaces: "-save c:\a file.txt -close"

Joe
 
the file name that has spaces will need to be passed with quotes ("") around it so let's say the arguments that gets passed to your app are:
C:\YourApp.exe
-Save
"C:\A File.txt"
-Close

which means on the commandline it looks like this:
C:\YourApp -Save "C:\A File.txt" -Close

you would read them in something like this:
VB.NET:
Private m_blnClose As Boolean = False
Private m_strFileName As String = ""

Private Sub Form_Load (...) Handles Me.Load
  For Counter As Integer = 1 To Environment.GetCommandLineArgs.GetUpperBound(0)
    Select Case CStr(Environment.GetCommandLineArgs(Counter)).ToLower
      Case "-save"
        m_FileName=CStr(Environment.GetCommandLineArgs(Counter + 1))
      Case "-close"
        m_blnClose = True
    End Select
  Next str
End Sub
 
Yes, that is how you have to pass arguments containing spaces to a executable call, such arguments have to be quoted to be counted as a single argument.
 
Back
Top