Hi,

I am developing an application where in I am using both forms and command line arguments. I have tried using Environment.getCommandLineArgs(). But it is reading only one argument, the path of the exe file. The command line arguments are first read and if present they are executed. Or else the form must be displayed to the user. The code is working fine if executed in the Visual Studio. But it is reading only one argument if the exe is used for the execution. Can any one locate the problem????


Thanks in advance,

Mohana

Recommended Answers

All 12 Replies

The first line of your program is probably static void Main(string[] args) . args contains the command line arguments.

I have tried accessing the args. But the args length is shown as 1 though i gave 10 args. I tried executing the exe as follows in the command prompt:

setup.exe -darg1 -farg2 ......

I am using the switch case to determine the arguments to be used for specific functions using the variable at first eg. d,f etc.


Every time I execute, it is opening the form by default since the arguments are not read.

Post code please? I think the file with your Main method would be the most useful.

I have tried accessing the args. But the args length is shown as 1 though i gave 10 args.

This

static void Main(string[] args) {
    foreach (String s in args) {
        Console.WriteLine(s);
    }
}

Displays all the command line arguments I can throw at it. You'll have to post your code as you seem to be doing something wrong.

Here is the code.

static void Main(string[] args)
        {
           try
            {
                cmdParser commands = new CmdParser(args);
                switch (commands.IsPendingActions)
                {
                    case true:
                        commands.Execute();
                        break;
                    case false:

                        Console.WriteLine("Starting GUI...");

                        if (commands.CalibrationTopDir != null)
                        {
                            Application.Run(new FormMain(commands.CalibrationTopDir));
                        }
                        else
                        {
                            Application.Run(new FormMain());
                        }

                        break;
                }
            }
       }

The arguments must be executed if present. Or else the form must be shown

Since the code to handle the arguments is in the class CmdParser, it might be a good idea to post that code.

If you open the properties of your console app and open the debug tab in the window that shows, you can set some command line arguments to test things out.
Strange that you have to use some weird thing like a CmdParser, when all the command line arguments are passed to you via the args string array as Momerath already pointed out. :-/

But the args are not read. ThecmdLineParser is not the problem.I checked the args by giving dislpay statements. The args length was displayed as 1 and the path of the excecutable was displayed.But I tried executing the same in visual studio. The args are passed properly. The application is a windows application and not console.

Since it's the code that reads the arguments it most definatly is the problem. Again, I have no problems reading multiple command line arguments in either windows or console application.

I checked the length of the arguments before passing the same to the cmdLineParser. It showed 1. It is not reading the arguments i guess. Can u pls tell me the syntax for the code execution with command line arguments???? May be the syntax is wrong. I have to pass 9 parameters along with the exe file during execution

private string fn
        {
            get
            {
                string outstr = "";
                string[] commands = Environment.GetCommandLineArgs();
                if (commands.Length == 2)
                {
                    outstr = commands[1];
                }
                return outstr;
            }
        }

to call the method just call like this

string s = fn;

now do whatever you need with string s;

Thanks alot. i will definitely try that. Can you please guide me in the execution? Can u tell me how to give the parameters along with the exe name????

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.