Answered by:
Command Line Argument errors

Question
-
Hello again!
I have a program that i made to open a custom file. To allow it to open the file from command line args., in the application events file, i added some code to create a new instance of this program for each argument passed to the app.(Each argument is the path to a different file, or if there is just 1 argument, it does not start any more instances of the program). To start a new instance of the program, i use Process.start to start a new instance and pass the argument. However, when i tried it out by starting the program with multiple command line arguments, for some reason, everytime there is a space in the filename(like in 'My Documents'), the program counts it as multiple command line arguments.
So for example:
if i pass the file path "C:\Users\Kevin\Pictures\Program Images\Some File.bmp" to the program, (and this is where it gets confusing), it works just fine! But if i pass multiple filenames to the program, then the errors start. So if i pass the same argument as before as well as a similarly named file(in the same directory), the program will load and for each argument, it will start 1 less instances of this program so that each instance is set to load 1 file. The first instance(which is the one that will also start all the other instances) loads just fine and loads the first file. However, the next instance loads but it separates the argument into several arguments based on how many spaces there are.
If i haven't confused you yet!, here is the code i use to in the application's startup event:Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup Dim c As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs 'get the path of the program so it can start it. Dim p As String = My.Application.Info.DirectoryPath & "\" & My.Application.Info.AssemblyName & ".exe" If c.Count = 0 Then Else For i = 1 To c.Count If i = 1 Then 'now use the LoadFile procedure in Form1 to open the file. Form1.LoadFile(c.Item(0)) Else Process.Start(p, c.Item(i - 1)) End If Next End If End Sub
Thanks for any help!
Kevin Schaefer "The world is round and the place which may seem like the end may also be the beginning"- Changed type nobugz Saturday, August 1, 2009 5:57 PM Marking answered
Saturday, August 1, 2009 3:52 AM
Answers
-
Put quotes around the argument:
Process.Start(p, """" + c.Item(i-1) + """")
John, you surprise me.
Hans Passant.- Marked as answer by nobugz Saturday, August 1, 2009 5:57 PM
Saturday, August 1, 2009 4:41 AM
All replies
-
Create a single instance application. Then, pass the arguments to it, since it will be open, it will process the arguments, if it is closed it will open and process the arguments.
Even in C#, we use the VB framework to handle this
Single-Instance C# Application
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.comSaturday, August 1, 2009 4:28 AM -
Put quotes around the argument:
Process.Start(p, """" + c.Item(i-1) + """")
John, you surprise me.
Hans Passant.- Marked as answer by nobugz Saturday, August 1, 2009 5:57 PM
Saturday, August 1, 2009 4:41 AM -
Well Hans, I have learned a lot from you. Also, you have taught me to open my eyes and leverage the whole library. I still am an avid reader of your posts, whether you know it or not and have answered a lot of my questions without me asking them.
Thank you for your patience with me all these years.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.comSaturday, August 1, 2009 3:28 PM -
Thanks nobugz, that worked perfectly!!
Kevin Schaefer "The world is round and the place which may seem like the end may also be the beginning"Saturday, August 1, 2009 5:48 PM -
That's great, thanks John.
Hans Passant.Saturday, August 1, 2009 5:58 PM