20

I am making a WPF application and I want to release a beta version of the application, for that I am adding a Button named "debug" Which will essentially show/hide the console window. I am writing appropriate message on the console after each event occurs so This will help users to report back the problem that they are having by looking at the messages on the console .

Sorry for the background story (if it's not helpful). I essentially need to know how to show/hide console windows dynamically in c# .

3
  • 1
    Have you considered using a "real" logger like log4net instead of console outputs? logging.apache.org/log4net
    – Tim S.
    Jul 22, 2012 at 12:45
  • Yup , but for my level of requirement. I am comfortable with the console output .
    – rajat
    Jul 22, 2012 at 12:48
  • Why don't you just open a WPF window that shows the debug lines you want to show? You can clear it on close and write while it is opened. Jul 22, 2012 at 13:03

1 Answer 1

37

Do you think now I understood the question?

[DllImport("Kernel32")]
public static extern void AllocConsole();

[DllImport("Kernel32")]
public static extern void FreeConsole();


private void button1_Click(object sender, EventArgs e)
{
    AllocConsole();
    Console.WriteLine("test");
}
3
  • Yup , You hit the bulls eye .
    – rajat
    Jul 22, 2012 at 13:14
  • Is there a way to make closing the console window just close the console window and not the entire application?
    – zenonet
    Jul 15, 2022 at 12:10
  • @zenonet just call FreeConsole(). Sep 5 at 4:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.