Console Output Util : Console « Development Class « C# / C Sharp






Console Output Util

        
// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

//---------------------------------------------------------------------------------
// Microsoft (R) .NET Services
// Software Development Kit
// 
// Copyright (c) Microsoft Corporation. All rights reserved.  
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. 
//---------------------------------------------------------------------------------

namespace Microsoft.AccessControl.Samples.AcmTool
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Security;
    using System.Security.Permissions;
    using System.Text;

    internal enum PrintFormat
    {
        Error,
        Warning,
        Header,
        SubHeader,
        Message,
        Success
    }

    internal class ConsoleUtil
    {
        internal static void WriteMessage()
        {
            WriteMessage(PrintFormat.Message, String.Empty);
        }

        internal static void WriteMessage(string format, params object[] args)
        {
            WriteMessage(PrintFormat.Message, format, args);
        }

        internal static void WriteMessage(PrintFormat printFormat, string format, params object[] args)
        {
            switch (printFormat)
            {
                case PrintFormat.Error:
                    Write(ConsoleColor.Red, true, format, args);
                    break;
                case PrintFormat.Header:
                    Write(ConsoleColor.Cyan, false, format, args);
                    break;
                case PrintFormat.Message:
                    Write(ConsoleColor.Gray, false, format, args);
                    break;
                case PrintFormat.SubHeader:
                    Write(ConsoleColor.White, false, format, args);
                    break;
                case PrintFormat.Success:
                    Write(ConsoleColor.Green, false, format, args);
                    break;
                case PrintFormat.Warning:
                    Write(ConsoleColor.Yellow, true, format, args);
                    break;
            }
        }

        private static void Write(ConsoleColor foregroundColor, bool isError, string format, params object[] args)
        {
            ConsoleColor beforeColor = Console.ForegroundColor;
            Console.ForegroundColor = foregroundColor;

            if (!isError)
            {
                Console.WriteLine(format, args);
            }
            else
            {
                Console.Error.WriteLine(format, args);
            }

            Console.ForegroundColor = beforeColor;
        }
    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Echo some stats
2.Read a line of string and check its length
3.WriteLine and Write
4.Printing one line of text with multiple statements.
5.Printing multiple lines with a single statement.
6.Printing multiple lines of text with string formatting.
7.Displaying the sum of two numbers input from the keyboard.
8.Change console foreground color and background colorChange console foreground color and background color
9.Clear consoleClear console
10.Set console: title, window size, buffer height and width, cursor positionSet console: title, window size, buffer height and width, cursor position
11.CursorVisible ResetColor SetWindowSize BufferHeight BufferWidth CursorLeft CursorSize CursorTop
12.Use Red and Green from ConsoleColor
13.Beep
14.Beep(200, 300)
15.Use format specifiers with Console.WriteLine
16.If the console moves, Pause.