How can I access parameters given from the Command Line?

If I choose to run a program the following way

Start > Run
"MyCSharpApp.exe" "Some Text"

How do I access "Some Text"

TIA

Mike



Answer this question

How can I access parameters given from the Command Line?

  • suranga_d

    hi.,

    It is work when I do like this:

    I went to the start menu - run and typed

    C:\MyProgram Folder\AppPath.exe param

    The output is

    Param 0 = param

    I don't know why yours not work. May be you didn't specify the filepath in the Run.

    my code is here.

    static void Main(string[] args)

    {

    for(int i = 0; i < args.Length; i++)

    {

    Console.WriteLine("Param {0} = {1}", i, argsIdea);

    }

    Console.Read();

    }

    :)

    soemoe




  • newjdevil

    If you are creating a Windows application, than unfortunally Console.WriteLn does not do anything. While in the debugger you see the output in the output window, but if started from the commandline it doesn't. The Windows app does not have a hook to the commandline.

    Of course if you made a console application it will work.


  • Francis Shanahan

    Like Marcel already wrote - if you created Windows app, try outputing your parameters some other way - maybe by showing a message box for each parameter:

    for(int i = 0; i < args.Length; i++)
    {
      MessageBox.Show(string.Format("Param {0} = {1}", i, args[ i ]));
    }

    Andrej



  • japt

    Could you be more specific with what didn't work The Main method should be application's default entry point which gets called every time the application is started.

    [The default Main method is located in the static Program class in Program.cs].

    Andrej



  • Jesse Towner

    Well, I went to the start menu - run and typed

    "AppPath.exe" "ParamHere"

    The program code look somthing like this

    for(iny i = 0; i < args.Length; i++)
    {
    Console.WriteLine("Param {0} = {1}", i, args[ i ]);
    }

    The program wrote nothing...

    Any Ideas

    Thanx
    Mike


  • AnilK110285

    Didn't work, any other thoughts

    TIA
    Mike


  • Keith Henkel

    Hi,

    try the following:

    [STAThread]
    static void Main(string
    [] args)
    {
       
    if
    (args.Length > 0)
        {
           
    string
    firstParameter = args[0];
        }
    }

    Andrej



  • How can I access parameters given from the Command Line?