4

I have C# Windows form application, I am doing a lot of check boxes and write and read them to JSON format file called test.json. Now I want to use my test.json with program.exe, that my program.exe would get check box checked as written in test.json. So I create Load event handler and want to use my GetCommandLineArgs().

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

I know that I have 2arguments.[0] - program.exe and [1] - test.json. Need some ideas how to make it work.

My question is:

How to make that args list of 2elements. Where 0 is program.exe and 1 is test.json. And how to work with args.length when there is possibility that there will be no parameters or only one parameter

2
  • Send json file path in second argument Commented Nov 18, 2015 at 12:53
  • You mean var path = args [1] ? Commented Nov 18, 2015 at 12:57

2 Answers 2

4

How to make that args list of 2 elements. Where 0 is program.exe and 1 is test.json

To do this you can debug from within visual studio by opening the project properties window and navigating to the Debug tab, from within there you can specify the command line args you want as shown below:

Entering command line arguments for debugging

You can use the Environment.GetCommandLineArgs(), it is a little odd in my opinion. It provides to you the executable name that is currently running. So in your case you would end up with exactly what you're asking for.

[0] - program.exe and [1] - test.json 

How to work with args.Length when there is possibility that there will be no parameters or only one parameter.

This is simple, the Environment.GetCommandLineArgs() returns a string[]. Check for the desired length and handle it accordingly.

var commandLineArgs = Environment.GetCommandLineArgs(); if (commandLineArgs.Length > 0) { // Do something magical... } else { // Nothing was passed in... } 
Sign up to request clarification or add additional context in comments.

6 Comments

Is there any possibility not touching main method? I want to everything in that Load event handler. And another question how to handle it? If I know that my args.Length = 1 is no parameters given and args.Length = 2 , only one parameter given.
Thanks its very informative and helpful, but when I use my command line it just simply runs my program.exe nothing specifically happens. Maybe I am doing something wrong? Even if I put only program.exe or I put also test.json it runs program.exe simply.
I believe I did correctly answer your specific questions, with that said. It sounds like you might have some other issues -- perhaps, the test.json file is missing or not found locally, or something else. I would propose that you accept this answer and create another one specific to the other issues you're seeing.
Okey, last question. I am doing coding in one directory and my project where I put my program.exe and test.json is in another directory. Is this could influence my problem?
Yes, that is an issue... The test.json file must either be located in the same directory where the program.exe is executing, unless you fully qualify the file path. Please accept my answer if you think it is correct.
|
3

Something like this:

var cmdArgs = Environment.GetCommandLineArgs(); if (cmdArgs.Length < 2) { MessageBox.Show("No JSON file specified!"); } var jsonFilename = cmdArgs[1]; 

If you do more complex command line parameter parsing I suggest to use an existing library like this one.

Update:

Here is where you can attach your event-handler (or create a new one doing a double-click):

enter image description here

7 Comments

I liked you offer :) but still when i use my command line, it works same as without Getcommandline() . It makes my program run simply. Maybe I am doing something wrong?
Did you have a look with the debugger? See David Pines answer how to set the command line arguments in "Debug" within Visual Studio. Then set a breakpoint on var cmdArgs = ..... Do you get your parameter in there?
I've testet the above code in a Winforms app. Works as expected if you put it into your FormLoad eventhandler. What exactly is the problem?
I think my problem is I don't know how to make my event holder load. It should be something like this.Load += Form1_Load;
Yes that's right. You can do this without coding: select the form in the designer. To to "Properties" (F4) and switch to the Tab "Events" and search for the event called "Load". Choose your Form1_Load() method from the dropdown. If you don't have one yet: double-click the input box and Visual Studio will create you a new one and register it with +=.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.