1

I have sort of strange question.

I've created a form application, with menus, options, buttons etc. Also I've implemented possibility to turn on and off some options using arguments and launching application from Command Prompt. Now I would like to implement reaction to additional "help" argument, I want it to show information about all the possible arguments and some examples.

Is there way to show some output to console I am currently running from, without creating additional console? Or it would be just easier just to show new MessageBox with description of all the arguments?

Thank you!

1
  • In general. You have already a winforms application. Why would You like to mix user-interface-types and suddenly open a console, to show information in it, about how to use the winforms app ? Implement a "help" toolbar-button into Your winforms applicatoin. I would go for this, and I would not follow the option to customize the behaviour of a winforms application by commandline-args. I would more or less, make it all either interactive or use a settings-mode to customize it. Commented Jul 20, 2015 at 10:01

2 Answers 2

1

If there is no important reason why you would use console - I would just use MessageBox.

Mixing console and windows forms is not good idea.

If you really have to do it - there is AttachConsole function in kernel32.dll. You can use it like this:

Program.cs file:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace Test
{
    static class Program
    {
        [DllImport("kernel32.dll")]
        static extern bool AttachConsole(int dwProcessId);
        private const int ATTACH_PARENT_PROCESS = -1;

        [STAThread]
        static void Main()
        {
            AttachConsole(ATTACH_PARENT_PROCESS);
            Console.WriteLine("This will show on console.");

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new Form1());
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply! Unfortunatelly, this is not exactly what I was asking: I would like to run my application from console and then output to the same console some information. Anyway, thanks again for your answer!
And this is it. It will attach console which is parent process of your application instead of creating new console.
Ah, my mistake, I was confused by AttachConsole, I was trying to use AllocConsole(). Thank you!
1

This is what I use to add the console to applications when I've needed it:

#region Console support
[System.Runtime.InteropServices.DllImport("Kernel32")]
public static extern void AllocConsole();

[System.Runtime.InteropServices.DllImport("Kernel32")]
public static extern void FreeConsole();
#endregion

When we need to turn it on, we can call AllocConsole(), and likewise FreeConsole() when we want to turn it back off.

As well, I created/use the following to write to the console with color:

/// <summary>
/// Writes to the Console. Does not terminate line, subsequent write is on right of same line.
/// </summary>
/// <param name="color">The color that you want to write to the line with.</param>
/// <param name="text">The text that you want to write to the console.</param>
public static void ColoredConsoleWrite(ConsoleColor color, string text)
{
    ConsoleColor originalColor = Console.ForegroundColor;
    Console.ForegroundColor = color;
    Console.Write(text);
    Console.ForegroundColor = originalColor;
}

/// <summary>
/// Writes to the Console. Terminates line, subsequent write goes to new line.
/// </summary>
/// <param name="color">The color that you want to write to the line with.</param>
/// <param name="text">The text that you want to write to the console.</param>
public static void ColoredConsoleWriteLine(ConsoleColor color, string text)
{
    ConsoleColor originalColor = Console.ForegroundColor;
    Console.ForegroundColor = color;
    Console.WriteLine(text);
    Console.ForegroundColor = originalColor;
}

Example usage:

#region User Check
Console.Write("User: {0} ... ", Environment.UserName);
if (validUser(Environment.UserName).Equals(false))
{
    ColoredConsoleWrite(ConsoleColor.Red, "BAD!");
    Console.WriteLine(" - Making like a tree, and getting out of here!");
    Environment.Exit(0);
}
ColoredConsoleWrite(ConsoleColor.Green, "GOOD!"); Console.WriteLine(" - Continue on!");
#endregion

Valid user output with "GOOD!" being in Green text:

User: Chase.Ring ... GOOD! - Continue on!

Invalid user output with "BAD!" being in Red text:

User: Not.Chase.Ring ... BAD! - Making like a tree, and getting out of here!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.