0

I am working on Windows Form (C# application), and when I run this application from cmd.exe, the message (error log, debug log) is output to message box.

How should I do to output this message to current CMD.exe which I ran C# appliation?

Example: I do

C:>D:\Demo\CsOutputMsgToCMD\CsOutputMsgToCMD\bin\Release\CsOutputMsgToCMD.exe 1

Output: This message must be output in current CMD

The argument is 1

Update #1: My C# application is Windows Form application (Not console) But I want when user run this application from cmd.exe

  • Case #1: Have NOT any argument: It launch the application normally (GUI)
  • Case #2: Have argument

    • If argument == 1, I do action #1 without launch GUI
    • If argument == 2, I do action #2 without launch GUI
    • If argument == n, I do action #n without launch GUI

    => When not launch GUI app, the output must be in current cmd.exe (which ran application)

GSP
  • 574
  • 3
  • 7
  • 34
  • 3
    Use a Console application template in that case. – Rahul Jan 22 '16 at 07:20
  • 1
    Aside from the fact that, as a windows application you don't have to be launched from a console window (so none may exist), you'll also notice that the console from which you launched is ready and able to launch new commands - so it may be in the middle of performing completely unrelated output for some other program. – Damien_The_Unbeliever Jan 22 '16 at 07:21
  • You might be able to achieve that using a Debug listener and add it to the `Debug.Listeners` collection.. Also you need to define the DEBUG and/or TRACE build flags. – Mihail Stancescu Jan 22 '16 at 07:23
  • @Rahul, Damien_The_Unbeliever, Mihail Stancescu, Could you please take a look on my update #1? – GSP Jan 22 '16 at 07:30

2 Answers2

2

The common "trick" to making things work as per your update 1 is to have two programs. Lets call them devenv.com and devenv.exe. The .com variant is a console application. The .exe variant is a windows forms application.

If you just run devenv from a command prompt, the console application will run (since the command prompt favours .com over .exe when faced with two programs with the same name but different extensions). It can then inspect its arguments and, if it decides that the GUI is called for, it launches devenv.exe and then exits. Otherwise, it continues as normal for a console application and stays attached to the console.

If they will have a lot of common functionality, I'd recommend placing most of it in a DLL assembly that they can both share.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
1

One approach could be changing the project type to Console Applicaton and using Environment.GetCommandLineArgs() to get the arguments. So,

Step 1: Go to project properties -> Application and change Output Type to Console Application

Step 2: Change your main method to something like this:

static void Main()
{
    string[] args = Environment.GetCommandLineArgs();
    if (args.Length <= 1)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
    else
    {
        switch (args[1])
        {
            case "1": Console.WriteLine("Doing #1 stuff"); break;
            case "2": Console.WriteLine("Doing #2 stuff"); break;
            case "n": Console.WriteLine("Doing #n stuff"); break;
        }
    }
}

The first argument would be the name of the application and the others would be the actual parameters you send in.

You should of course extract the business logic to a separate DLL to avoid code duplication. This way if you run from the console without any parameters it will launch the Form1 window otherwise will check the argument value.

You can also use Console.WriteLine statements in the forms and that output will also go to the command prompt that launched the application.

One caveat with this approach is if you double-click the application a console window opens as well as the windows form. If it doesn't bother you or running it from the command prompt is the only option you'll give then it may work for you.

Hope this helps a bit.

Volkan Paksoy
  • 6,727
  • 5
  • 29
  • 40
  • This approach works fine when run application from cmd.exe. But when I run it by double-click, It launchs both GUI and Console. – GSP Jan 22 '16 at 08:29
  • @GSP: Yes, that's why I mentioned that in my answer. You can use interop to hide that window (haven't tried it myself though) This looks like having a working answer: http://stackoverflow.com/questions/3571627/show-hide-the-console-window-of-a-c-sharp-console-application – Volkan Paksoy Jan 22 '16 at 08:32