Solved

How to check for command-line argument that specifies the location of a file

Posted on 2008-10-22
4
259 Views
Last Modified: 2013-11-10
I need to take in a command-line argument that specifies the location of a file. This is for a desktop application, there is no interface, it being run completely by the command-line. I am not sure if I am on the right path.
Module Sample

    Sub main(ByVal args() As String)

        Dim dataCon As String = args(0)

        Console.WriteLine("Your input was: {0}", dataCon)

        Console.Write("Press Enter to close the program")

        Console.Read()

    End Sub

End Module

Open in new window

0
Comment
Question by:gvilla23
  • 2
4 Comments
 

Author Comment

by:gvilla23
ID: 22781785
For example the user would type this at the command line -
C:\Test\application.exe D:\somefolder\somexmlfile.xml
0
 
LVL 3

Expert Comment

by:TechSinger
ID: 22782087
That code looks fine.  It will only show you the first parameter that is given in the command line, however.
You can use the Environment.CommandLine command to capture the whole command line, which includes the path and filename of the executable that is run.
Once you parse the executable off, you have the raw data that is sent to the executable and not as parameters.  This would allow you to put spaces into the filename without needing quotes.  I have changed the code to reflect this and posted it with this comment.

Module Sample

    Dim filename As String

    Sub main(ByVal args() As String)

        Dim dataCon As String = Environment.CommandLine

        If dataCon.Substring(0, 1) = """" Then

            dataCon = dataCon.Substring(dataCon.IndexOf(""" ") + 2)

        Else

            dataCon = dataCon.Substring(dataCon.IndexOf(" ") + 1)

        End If

        Console.WriteLine("Your input was: {0}", dataCon)

        Console.Write("Press Enter to close the program")

        Console.Read()

    End Sub

End Module

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 22782187
Use a regular WINFORMS application and get the command line parameters from the Startup() Event:
http://msdn.microsoft.com/en-us/library/t4zch4d2(VS.80).aspx

It will properly return a filename as ONE string, even if it has spaces in it...as long as it is surrounded by quotes:

    C:\Test\application.exe "D:\some folder\with spaces\somexmlfile.xml"

You can keep the default Form from showing at all by setting the Cancel parameter to True.

To get these events...
Click on Project --> Propertes.
On the Application Tab, make sure the "Enable Application Framework" box is Checked.
In the Bottom Right, click on the "View Application Events" Button.
Across the top...
Change the Left DropDown to "(MyApplication Events)".
Change the Right DropDown to "Startup".

So your code might look like:
Namespace My
 

    ' The following events are availble for MyApplication:

    ' 

    ' Startup: Raised when the application starts, before the startup form is created.

    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.

    ' UnhandledException: Raised if the application encounters an unhandled exception.

    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 

    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.

    Partial Friend Class MyApplication
 

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup

            If e.CommandLine.Count > 0 Then

                Dim fileName As String = e.CommandLine(0)

            

                ' do something with "fileName"...

 

                MessageBox.Show(fileName)

            End If
 

            e.Cancel = True ' Shut app down WITHOUT ever showing a Form...

        End Sub
 

    End Class
 

End Namespace

Open in new window

0
 

Author Closing Comment

by:gvilla23
ID: 31508993
Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now