WinForm Program start Console window

Source: Internet
Author: User
Tags string format

This article reproduced: http://blog.csdn.net/oyi319/article/details/5753311

2.WinForm Program and Console window console

If you debug a SharpDevelop source program, you will see that it appears in debug mode with a console window to display the log information. Maybe I'm using a different approach, but you can try it out and write our own debug log code.

The first issue to solve is how to display the console window in debug mode. I am sure that this is a WinForm project and has not changed its output type. We need to use some API functions at the entry point of the project to display the console:

They are allocconsole and freeconsole.

    1. [DllImport ("kernel32.dll")]
    2. Public static extern Boolean allocconsole ();
    3. [DllImport ("kernel32.dll")]
    4. Public static extern Boolean freeconsole ();

We then make it determine the debug compile tag at the beginning of main (), call the AllocConsole method to display the console, and then, at the end of Main (), judge the debug compile tag and call the Freeconsole method to close the console. In this way, we can use Console.Write and other methods to display debug information in this console window.

To achieve better results, we write a shell class that encapsulates the Console.WriteLine method and outputs the personalization information. I do this, according to the output to the console text in the first few words to be judged as "warning", "error", "note" When the output with yellow, red, green text, the other output information output console default gray text, in order to differentiate the effect, but also before each message with the output of the time of the information.

This is the Shell class:

  1. <summary>
  2. Interacting with the console
  3. </summary>
  4. Static class Shell
  5. {
  6. // <summary>
  7. /// Output Information
  8. // </summary>
  9. /// <param name= "format" ></param>
  10. /// <param name= "args" ></param>
  11. public static void WriteLine (string format, params object[] args)
  12. {
  13. WriteLine (string.  Format (format, args));
  14. }
  15. // <summary>
  16. /// Output Information
  17. // </summary>
  18. /// <param name= "Output" ></param>
  19. public static void WriteLine (string output)
  20. {
  21. Console.foregroundcolor = Getconsolecolor (output);
  22. Console.WriteLine (@"[{0}]{1}", Datetimeoffset.now, Output);
  23. }
  24. // <summary>
  25. /// Select console text color based on output text
  26. // </summary>
  27. /// <param name= "Output" ></param>
  28. // <returns></returns>
  29. private static Consolecolor getconsolecolor (string output)
  30. {
  31. if (output.  StartsWith ("Warning")) return consolecolor.yellow;
  32. if (output.  StartsWith ("error")) return consolecolor.red;
  33. if (output.  StartsWith ("note")) return consolecolor.green;
  34. return consolecolor.gray;
  35. }
  36. }

Then the program entry function main code is as follows:

  1. <summary>
  2. The main entry point for the application.
  3. </summary>
  4. [STAThread]
  5. static void Main ()
  6. {
  7. #if DEBUG
  8. AllocConsole ();
  9. Shell.writeline ("NOTE: Start program ...");
  10. Shell.writeline ("/twritten by Oyi319");
  11. Shell.writeline ("/tblog:http://blog.csdn.com/oyi319");
  12. Shell.writeline ("{0}:{1}", " Warning", " this is a warning message.  ");
  13. Shell.writeline ("{0}:{1}", " error", " this is an error message!")  ");
  14. Shell.writeline ("{0}:{1}", "note", "This is a required attention message.  ");
  15. Shell.writeline ("");
  16. #endif
  17. Application.enablevisualstyles ();
  18. Application.setcompatibletextrenderingdefault (false);
  19. Application.Run (new Form1 ());
  20. #if DEBUG
  21. Shell.writeline ("Note: 2 seconds to close ...");
  22. Thread.Sleep (2000);
  23. Freeconsole ();
  24. #endif
  25. }

Now this console window will only be displayed in debug mode, and will not appear at release compile time. Is this the debug method you want?

WinForm Program start Console window

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.