-2

I have an Console Application that looks similar to this:

static void Main(string[] args)
{
   if(args.Length == 0)
   {
      //do something
   }
   else if(args.Length % 2 == 0)
   {
      //do something else
   }
}

Now when I build and start my .exe file it just opens for an blink of an eye to run the code and closes.

Is it possible that the .exe waits for me to enter parameters after starting it?

I know I can open cmd and write the directory to my file and write my parameters (C:\Example\MyExe.exe -Parameter1). But is this the only way?

axbeit
  • 853
  • 11
  • 35
  • 6
    Visual Studio has a page in the project properties where you can put default arguments for your app. Otherwise you need to code a series of Console.ReadLine to get the input you want directly inside the application – Steve Apr 02 '19 at 12:57
  • 4
    It won't wait for `args` since these are passed when you start the application, but you could implement your app to accept user input by using `Console.ReadLine `. – mm8 Apr 02 '19 at 12:57
  • 1
    There is no code that you have shown which will make the console wait for an input. Unlike some other scripting IDEs, you will have to code if you want to be prompted. If your inquiry is about how to instruct visual studio to provide command line argument to your app, then you should go to project properties and in the debug tab, there is command line arguments box where you can enter this information – Vikhram Apr 02 '19 at 12:58
  • 1
    Possible duplicate of [Why is the console window closing immediately once displayed my output?](https://stackoverflow.com/questions/8868338/why-is-the-console-window-closing-immediately-once-displayed-my-output) – Owen Pauling Apr 02 '19 at 13:02
  • Hint: you can hit CTR+F5 (Run without debugger) to run console application. This will automatically wait for keypress before closing console window. This way you can't test "do something else". – Sinatr Apr 02 '19 at 13:08

6 Answers6

4

Is it possible that the .exe waits for me to enter parameters after starting it?

It won't wait for args since these are supposed to be passed in when you start the application, but you could implement your app to accept user input using Console.ReadLine. Here is an example:

static void Main(string[] args)
{
    if (args.Length == 0)
    {
        List<string> arguments = new List<string>();
        do
        {
            Console.WriteLine("Input argument and press <ENTER>: ");
            string argument = Console.ReadLine();
            if (string.IsNullOrEmpty(argument))
                break;
            arguments.Add(argument);
        } while (true);

        Console.WriteLine("continue...");
    }
    else if (args.Length % 2 == 0)
    {
        //do something else
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
1

If your application opens and immediatly shuts down, that's because there is no "blocking" method. You can add a Console.Read(); at the end that will wait for a key to be pressed.

In example :

static void Main(string[] args)
{
   if(args.Length == 0)
   {
      //do something
   }
   else if(args.Length % 2 == 0)
   {
      //do something else
   }

   Console.WriteLine("Press any key to exit the application.");
   Console.Read();
}

To pass parameters, I'm used to use cmd and pass directly the arguments to the exe, but this can be done in VS

Right click on project -> Properties -> Debug -> Command Line Arguments

Cid
  • 14,968
  • 4
  • 30
  • 45
1

Yes this is the only way because the parameters are passed when starting the application.

You can add debug parameters in Visual Studio if you want to debug it. For this right click on the project and open the settings of the project. Then you can add them in the debug section.

Have a look at Passing command line parameters with Visual Studio C# for further information.

Further info: if you want let your application stay open till the user presses a key you can use Console.Read(); for this.

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51
  • The Command Line Arguments look promising. But my problem here is that it could be possible that no arguments are passed or up to 10 arguments. I guess it can't be solved with debug parameters? – axbeit Apr 02 '19 at 13:16
  • @axbeit Whats the problem with no parameters? Your code `if(args.Length == 0)` will be executed. Seems like you are having more then one issue. Do you want start up parameters (like you used in your code) or do you want user input as parameters? – Mighty Badaboom Apr 02 '19 at 13:19
  • Okay so I put into the Command line arguments this: 'Argument1 Argument2 Argument3'. Then I build my project. double clicked MyExe.exe the Window just shortly pops up and closes. But I want it so instead of closing it allows me to write my arguments. – axbeit Apr 02 '19 at 13:24
  • Just have a look at the answer from mm8. And be more specific with your next question ;) It is a lot easier when we know what you're trying to achieve instead of asking you. – Mighty Badaboom Apr 02 '19 at 13:30
1
  1. Right-click the default project
  2. Select "Properties"
  3. Click on the "Debug" tab on the left.
  4. Type your command line arguments in "Command line arguments".
  5. Save the updated properties and run the project.
piatkosia
  • 114
  • 2
  • 4
  • The Command Line Arguments look promising. But my problem here is that it could be possible that no arguments are passed or up to 10 arguments. I guess it can't be solved with debug parameters? – axbeit Apr 02 '19 at 13:16
  • In this case you can create 2nd app and run Process.Start for your application with your parameters (or not). – piatkosia Apr 02 '19 at 13:23
1

If you add the line:

Console.ReadLine();

the console will remain opened until a key is pressed.

So your code should be:

static void Main(string[] args)
{
   if(args.Length == 0)
   {
      //do something
   }
   else if(args.Length % 2 == 0)
   {
      //do something else
   }
   Console.ReadLine();
}

To pass some params through Visual Studio: right click on the project -> Properties -> Debug -> Command Line Arguments

Dave
  • 1,912
  • 4
  • 16
  • 34
0

Write Console.Readkey() after your else if() {} finish...

console will not close until you press enter

patel vinay
  • 85
  • 1
  • 1
  • 8