How do I access a command line argument from my application called through a .bat file?

I'm writing a desktop application in VS 2012 C# and am wanting to know if there is another way to access a command line argument? Specifically, and maybe I'm missing some parts of this equation so i'm looking for some guidance, what I'd like to do, and what I thought I've seen done in the past is:

1.

Set a shortcut to my application on the desktop

2.

Right click on the icon, and set edit the properts (the target: property) to run a .bat file

3.

in that .bat file pass a command line argument to the application

4.

Inside my form_Load event, access that command line argument
This is not a console application. Is this even possible? Or would it just be easier to include a .txt file in the folder where the executable lives and have my application read that text file? If it is possible, what would the command inthe .bat file look like?
LVL 1
Michael SterlingWeb Applications DeveloperAsked:
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.

Kyle AbrahamsSenior .Net DeveloperCommented:
From:
http://www.howtogeek.com/howto/programming/get-command-line-arguments-in-a-windows-forms-application/

string[] args = Environment.GetCommandLineArgs();

Then you have an array that has your args.

Note there's no need for a bat file to do this, you can edit the shortcut directly to pass in the args, but either way the final output of the command line call would look something like:

"C:\program files\My Program Folder\MyProgram.Exe Param1 Param2 ParamN"

(or use x86 if you're building a 32 bit app in a 64 bit OS)
0
Michael SterlingWeb Applications DeveloperAuthor Commented:
@Kyle: Thank you. I should've mentioned, the users won't be using a command line either, they will be double clicking on the icon. So, with that being said, can I alter the target path in the the properties dialog to something like what you have above? Or where exactly do I edit the shortcut to take in parameters properly? Right now when I add a value after the .exe in the target property, I get an error telling me I have an invalid directory path.
0
Michael SterlingWeb Applications DeveloperAuthor Commented:
@Kyle: the image contains what my target property looks like and what I'm getting when I double click the icon.

Invalid Directory Error
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Kyle AbrahamsSenior .Net DeveloperCommented:
Can you confirm there's actually a c:\Dev\SendGL\bin\release folder?

Also it says the path sohuld end with  a \ . . . so change the Start In to be

c:\Dev\SendGL\bin\Release\

For the target I would make it:

"c:\Dev\SendGL\bin\release\SendGL.exe T" just to be on the safe side.

If you take out the "T" does it launch fine?
0
Michael SterlingWeb Applications DeveloperAuthor Commented:
@Kyle: the path does exist, i did check. when I change the target to what you suggest I get:

double quote error
Yes, when I take the "T" out, it launches fine. Also, if I take the "T" out, but leave the path enclosed in quotes, when I click "apply", it automatically removes the quotes. Just thought I'd mention that for what ever its worth.
0
Duncan RoeSoftware DeveloperCommented:
You only need to enclose the path in quotes if it contains spaces, which your path doesn't.
You must have un-quoted space between path and command-line argument(s)
0
Michael SterlingWeb Applications DeveloperAuthor Commented:
@Duncan: you mean as in my original picture above? is that the way it should look in the Target Field?
0
Duncan RoeSoftware DeveloperCommented:
Yes, it looks fine to me.
But, it didn't work for you. You could try "c:\Dev\SendGL\bin\release\SendGL.exe" "T" and see which quotes it removes when you click Apply.
Also, what happens when you click the Open File Location button?
0
Michael SterlingWeb Applications DeveloperAuthor Commented:
@Duncan: When I put quotes around both, and click apply it removes the quotes from around the path, but leaves them around the parameter, but I get the same error when I try to run the application.

When I click on the Open File Location button it opens up to my release directory for the application (see included image).

open file location
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Just to isolate this can you try launching it from a command prompt:

Start -> run -> cmd
cd c:\dev\sendGL\bin\release

SendGL.exe T

Open in new window

and ensure that runs and you can read the param?
0
Michael SterlingWeb Applications DeveloperAuthor Commented:
@Kyle: i got the following from running it through the command prompt:

command prompt execute
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Looks like that's coming from the program itself.

Search on "invalid directory" in your code.
0
Michael SterlingWeb Applications DeveloperAuthor Commented:
@Kyle: I searched my code for that and nothing came up.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
can you post your code for your form load?
0
Michael SterlingWeb Applications DeveloperAuthor Commented:
private void frmSend_Load(object sender, EventArgs e)
        {
           try
           {
                string[] args = Environment.GetCommandLineArgs();
                myGlobalVars.gbTEST_MODE = true;
                lblTestMode.Visible = true;
                myGlobalVars.gbSilentMode = false;
                myGlobalVars.gbIsReProc = false;
                myGlobalVars.gbIgnoreRefresh = true;
                try
                {
                    RegistryKey doesKeyExist = Registry.CurrentUser.OpenSubKey(@"Software\VB and VBA Program Settings\Send\General\", true);
                    if (doesKeyExist != null)
                    {
                        myGlobalVars.gsDirectory = Registry.GetValue(@"HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Send\General", "DefaultDirectory", "").ToString();
                        this.Text = doesKeyExist.GetValue("DefaultDirectory").ToString();
                    }
                    else
                    {
                        RegistryKey newKey = Registry.CurrentUser.CreateSubKey(@"Software\VB and VBA Program Settings\Send\General");
                        newKey.SetValue("DefaultDirectory", @"\\blah\blah\blah");
                        myGlobalVars.gsDirectory = Registry.GetValue(@"HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Send\General", "DefaultDirectory", "").ToString();
                        this.Text = myGlobalVars.gsDirectory.ToString();
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Building Registry Tree/Key ERROR: 'frmSend_Load' method: " + ex.Message.ToString());
                    writeToLogFile(ex.Message.ToString());
                }
                this.Text = "SENDGL Upload Utility (" + myGlobalVars.gsDirectory.ToString() + ")";

                RefreshFileList(true, false);
                myGlobalVars.gbIgnoreRefresh = false;
            }
            catch (Exception ex)
            {
writeToLogFile(ex.Message.ToString());
            }
        }

Open in new window

0
Kyle AbrahamsSenior .Net DeveloperCommented:
Can you add the lines just for a test:

private void frmSend_Load(object sender, EventArgs e)
        {
            string[] args = Environment.GetCommandLineArgs();
 

// BEGIN INSERT
           foreach  (string arg in args)
               MessageBox.Show(arg, "args");
          
           return;
           // the rest will be unexecuted
        
// END INSERT


            myGlobalVars.gbTEST_MODE = true;
                lblTestMode.Visible = true;
                myGlobalVars.gbSilentMode = false;
                myGlobalVars.gbIsReProc = false;
                myGlobalVars.gbIgnoreRefresh = true;
                try
                {
                    RegistryKey doesKeyExist = Registry.CurrentUser.OpenSubKey(@"Software\VB and VBA Program Settings\Send\General\", true);
                    if (doesKeyExist != null)
                    {
                        myGlobalVars.gsDirectory = Registry.GetValue(@"HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Send\General", "DefaultDirectory", "").ToString();
                        this.Text = doesKeyExist.GetValue("DefaultDirectory").ToString();
                    }
                    else
                    {
                        RegistryKey newKey = Registry.CurrentUser.CreateSubKey(@"Software\VB and VBA Program Settings\Send\General");
                        newKey.SetValue("DefaultDirectory", @"\\blah\blah\blah");
                        myGlobalVars.gsDirectory = Registry.GetValue(@"HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Send\General", "DefaultDirectory", "").ToString();
                        this.Text = myGlobalVars.gsDirectory.ToString();
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Building Registry Tree/Key ERROR: 'frmSend_Load' method: " + ex.Message.ToString());
                    writeToLogFile(ex.Message.ToString());
                }
                this.Text = "SENDGL Upload Utility (" + myGlobalVars.gsDirectory.ToString() + ")";

                RefreshFileList(true, false);
                myGlobalVars.gbIgnoreRefresh = false;
            }
            catch (Exception ex)
            {
writeToLogFile(ex.Message.ToString());
            }
        } 

Open in new window


            This will ensure you're reading the CommandLine correctly.  after that you can step through to troubleshoot where that error is coming from.  

Also check the value of: myGlobalVars.gsDirectory
0
Michael SterlingWeb Applications DeveloperAuthor Commented:
@Kyle: what ever the problem is, it is happening before this code (event) happens because after putting in your changes, I never even get to that code (the MessageBox never shows up), and the app doesn't even open up, but that part is not new.
0
Michael SterlingWeb Applications DeveloperAuthor Commented:
I did find this interesting. When I took the "T" out of the Target field of the properties, and left your code in for the args, and ran it I got the dialog below. What's interesting about this to me, is that I thought it would say: "...SendGL.exe" NOT "...SendGL.vshost.exe". Not sure if that has anything to do with it or not. Just seemed odd, odd enough to mention it, from my point of view.

args loop msg box
0
Kyle AbrahamsSenior .Net DeveloperCommented:
is FrmSend the first or only form?  Do you have any application startup code?
0

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
Michael SterlingWeb Applications DeveloperAuthor Commented:
@Kyle: there was some other start up code. I cleaned it up and now. if i put a parameter in the target property, it has to be surrounded by double quotes or the app wont open up. i've tried surrounding everthing with quotes (single or double) but when i click apply, I get this:

surrounding in quotes
if I leave off the parameter, it will show the args which is the path to the exe on my machine. it still doesn't seem to know how to pick up the argument. also if i don't put any quotes around anything, but put in a parameter, the executable, when double clicked to open up, won't open.
0
Michael SterlingWeb Applications DeveloperAuthor Commented:
@Kyle: Nevermind. I figured it out. Thank you for your help. I do appreciate it.
0
Michael SterlingWeb Applications DeveloperAuthor Commented:
Thank you.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
First try it with the command prompt as that's the most direct way to input your "T" into the GL for now.

Once you get that working you should be able to use:

"c:\Dev\SendGL\bin\release\SendGL.exe" T

for your target.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
You should post your solution for anyone else coming across this thread, glad you got it though.
0
Michael SterlingWeb Applications DeveloperAuthor Commented:
@Kyle: Agreed. Ultimately, the other start up code was just not opening the form after it found the args. There was some if else branching that took it out of the code before the frmSend frmSend1 = new frmSend() and frmSend1.ShowDialog() code was hit. Once i put that code in place it worked fine. Ultimately it was your prompting me to look for other start up code. So I would recommend that all start up code be thoroughly examined if this probem is encountered. Thanks again.
0
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
C#

From novice to tech pro — start learning today.