none
Show commandline Help from C# WinForm application RRS feed

  • Question

  • Hi,

    I have an application and I want to show commandline line help hen the user try:

    MyApplication.exe /?
    
    


    in the commandline..

    How can i do that please?

    Thanks,

    Jassim

    Sunday, August 4, 2019 4:39 AM

Answers

  • Just add some code to check for "/?" in args of Main method in Program.cs.

    If the string is matched, show a dialog that contains the syntax like when you run "wscript /?".

    If you want to display it in command line, you'd want to call AttachConsole() with -1 and then call AllocConsole() if failed with error code 5.

    • Marked as answer by Jassim Rahma Monday, August 5, 2019 11:16 PM
    Monday, August 5, 2019 1:24 AM
    Answerer

All replies

  • Just add some code to check for "/?" in args of Main method in Program.cs.

    If the string is matched, show a dialog that contains the syntax like when you run "wscript /?".

    If you want to display it in command line, you'd want to call AttachConsole() with -1 and then call AllocConsole() if failed with error code 5.

    • Marked as answer by Jassim Rahma Monday, August 5, 2019 11:16 PM
    Monday, August 5, 2019 1:24 AM
    Answerer
  • oh Thanks. It is working

    I have two more questions please

    1. How can I start my text just after the commandline the user entered, for example if you see this dir /?

    you can find that immediatelly after the line of c:\......\dir /? all the text comes

    but in my case it comes like this:

    2. My second question, how cna I let the console go back to C prompt instead of waiting a key to press?

    here is the code:

    if (args.Length > 0 && (args.Contains("/?") || args.Contains("/?")))
    {
        if (!AttachConsole(-1))
        {
            AllocConsole();
        }
        else
        {
            Console.WriteLine("Showing help now!");
    
            return;
        }
    }
    


    Thanks,

    Jassim

    Monday, August 5, 2019 11:34 PM
  • I found something that I wrote long ago, your code should read something like the following instead

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr GetConsoleWindow();
    
    if (args.Length > 0 && (args.Contains("/?") || args.Contains("/?")))
    {
        if (GetConsoleWindow() == IntPtr.Zero)
        {
            AllocConsole();
        }
    
        Console.WriteLine("Showing help now!");
    
        return;
    }

    So you don't need to check for error code. 

    [how can I let the console go back to C prompt instead of waiting a key to press]

    I think you mean that "wait for key press" thing is automatically added by the IDE when your invoke Debug command (something added around the time of VS2012 or 2013), you shouldn't see it when you run the EXE directly unless you explicitly added Console.ReadKey() or the sort.


    Tuesday, August 6, 2019 1:11 AM
    Answerer
  • I found something that I wrote long ago, your code should read something like the following instead

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr GetConsoleWindow();
    
    if (args.Length > 0 && (args.Contains("/?") || args.Contains("/?")))
    {
        if (GetConsoleWindow() == IntPtr.Zero)
        {
            AllocConsole();
        }
    
        Console.WriteLine("Showing help now!");
    
        return;
    }

    So you don't need to check for error code. 

    [how can I let the console go back to C prompt instead of waiting a key to press]

    I think you mean that "wait for key press" thing is automatically added by the IDE when your invoke Debug command (something added around the time of VS2012 or 2013), you shouldn't see it when you run the EXE directly unless you explicitly added Console.ReadKey() or the sort.


    I tried your code but it shows empty [nothing]

    Here is a sample project:

    https://www.softnames.com/temp/ConsoleText.zip

    Tuesday, August 6, 2019 9:19 AM
  • Seems your outputs are redirected to the "Output" window.

    I'd think it's probably better to just leave it "as is" if your project only use this to display help message because it won't justify the extra work to "beat the IDE to comply". You can just:
    1) Run command prompt and run "ConsoleText /?" from there to test the "already have console" case
    2) Create a shortcut to the EXE and add " /?" to the command line, then run it to test the "add /? by other means" case.

    If you'll use console for other case,on VS2013, Go to Project Properties -> Debug -> Enable Debuggers and make sure 'Enable Visual Studio hosting process' is checked. This seems can disable redirection there.

    If using older version of VS is not applicable to you (I see your project pointing v4.7.2), try OverrideRedirection() method in the previous paragraph link.

    Btw, after testing your code I fallback to the following because the GetConsoleWindow() methods seems always creating new window here (Win7 machine). It used to work in my old project in WinXP, don't know what had changed.

                else if (args.Length > 0 && (args.Contains("/?")))
                {
                    if (!AttachConsole(-1))
                    {
                        AllocConsole();
                    }
                    else
                    {
                        Console.WriteLine();
                    }
    
                    Console.WriteLine("Showing help now!");
                    Console.ReadKey();
                    return;
                }


    Tuesday, August 6, 2019 11:00 AM
    Answerer
  • oh no! so this leaves me with no solution?

    Then how all the other apps doing it?


    Tuesday, August 6, 2019 11:04 AM
  • Don't bother do anything, I think.

    It's only Visual Studio redirecting all output to its "Output" window, so it will leave no problem with actual usage.

    To verify whether the output is correct, you can just check the output in "Output" window.

    To verify whether the "console create/attach" logic is correct, you can check with the ways I've suggested, and that is "check-it run correct then you can forget it" thing, so not worth the trouble to add code for "debug in IDE only" problem.

    Wednesday, August 7, 2019 1:14 AM
    Answerer