Command line information in Environment.GetCommandLineArgs()

I have a program where the first line of code executed (apart from defining variables) after I press f8 is this.
Dim args() As String = Environment.GetCommandLineArgs()

Yet it returns values.  When I start up a blank program it doesn't return values.  Where does visual basic 2005 get the information from.
AlHal2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
The value at Index 0 (zero) is the name of the executable itself...

See: http://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs(VS.71).aspx

    "The first element is the executable file name, and the following zero or more elements contain the remaining command line arguments."
AlHal2Author Commented:
Where can I see what these remaining command line arguments are?
How can I change them?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Add a Breakpoint just after that line and then you can inspect them.   Or just enumerate them and Debug.Print() them.

How can you change them?  Thet are PASSED into your application.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

AlHal2Author Commented:
Thanks.

I'd understand if the program was called by a batch file, but I just started pressing f8 and running through the source code.
When I right click on the project and select the debug tab the command line arguments text box is blank.

What is passing these parameters?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
As stated in the documents, the first parameter is just the name of the executable.

That isn't being passed...it's inherent in the GetCommandLineArgs() function.

Anything passed will be after the exe name.

Are you saying that you have more than ONE parameter?  What are they?
AlHal2Author Commented:
One is the name of a config file.
The other is the environment of the database to which the program connects  (dev or production)
AlHal2Author Commented:
The executable is also vshost.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
"When I right click on the project and select the debug tab the command line arguments text box is blank."

I can't find anywhere else that the extra command line arguments could be coming from.  =\
CodeCruiserCommented:
>The executable is also vshost.

What if you run the exe file directly? The appname.exe.vshost is a special file run by the VS to enable debugging, breakpoints etc.
AlHal2Author Commented:
Thanks for the reply.  There is no difference between the source code and the executable.
CodeCruiserCommented:
>Thanks for the reply.  There is no difference between the source code and the executable.

Do you mean that if you run the application by clicking the appname.exe (not appname.exe.vshost) in the windows explorer, it still gets the parameters?
AlHal2Author Commented:
Apologies.  My previous reply was wrong.  The executable runs on a different machine to where I run the source code.  Looking at the live machine, if I double click the exectuable I get the error message "index was outside the bounds of the array".
If I click on the batch file the program runs normally.  The batch file gives the path of the config file and the domain (Production, QA or development).
Does this help?
CodeCruiserCommented:
Yes. It means that the executeable is not getting any extra command line arguments.

The error is caused probably by the code that deals with command line args.
AlHal2Author Commented:
Below is the code which deals with command line arguments.  It's the first piece of code the program runs.  What I don't understand is why I don't get an error message when I run the source code with no batch file.  The source code seems to know where to get the command line argument from.
        Dim args() As String = Environment.GetCommandLineArgs()
        Dim cfgpath As String
        Try
            cfgpath = args(1)
            domain = args(2)
            FileOpen(1, cfgpath, OpenMode.Input)
            gConnectionString = LineInput(1)
            FileClose(1)
            Me.Text = "Reuters Fixed Income concordance " + domain + " database"
        Catch ex As Exception
            MessageBox.Show(ex.Message, "unable to open config file. Please contact CTG support")
            Me.Close()
        End Try

        Panel1.Dock = DockStyle.Fill

Open in new window

CodeCruiserCommented:
           cfgpath = args(1)
            domain = args(2)

When you run the exe directly, you do not have those arguments(unless you supply them obviously). So the error is self explanatory. You should add some logic to check the number of parameters before you access them.
AlHal2Author Commented:
Why don't I get this error when I run the source code?
CodeCruiserCommented:
Use a messagebox to see what parameters are passed when the program is run from IDE.
AlHal2Author Commented:
I put the array into the watch window as I ran the source code.

args(0)= Filepath\FIconcordance.vshost.exe
args(1)=Filepath\config.txt
args(2)=dev

I don't know where it's getting this information from.
When I right click on the project and select the debug tab the command line arguments text box is blank.
CodeCruiserCommented:
Search for the term "Filepath\config.txt" in the entire solution.
Untitled.jpg
AlHal2Author Commented:
It wasn't found.
CodeCruiserCommented:
What is the start action on the debug tab? Attach a screenshot.
AlHal2Author Commented:
Attached.  Raising points to 500.
ExpertsExchange.doc

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CodeCruiserCommented:
This is beyond understanding now. I dont know of any other place where parameters could be supplied.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.