And the support class (this is all C# 2.0 or better btw)
Code:
public class ConsolePlus
{
private ConsoleColor _errorColour = ConsoleColor.Red;
/// <summary>
/// Writes to the console using the error 'style'.
/// </summary>
/// <param name="message">The message.</param>
/// <param name="args">The args.</param>
public static void WriteError(string message, params object[] args)
{
using (new ConsoleColorScope(ConsoleColor.Red))
Console.WriteLine(message, args);
}
/// <summary>
/// Gets or sets the error colour.
/// </summary>
/// <value>The error colour.</value>
public ConsoleColor ErrorColour
{
get { return _errorColour; }
set { _errorColour = value; }
}
/// <summary>
/// Sets the ConsoleColor, for only the time indended before reverting to the original.
/// </summary>
public class ConsoleColorScope : IDisposable
{
private readonly ConsoleColor _foregroundCol;
private readonly ConsoleColor _backgroundCol;
/// <summary>
/// Initializes a new instance of the <see cref="ConsoleColorScope"/> class.
/// </summary>
/// <param name="ForegroundColor">Color of the foreground.</param>
public ConsoleColorScope(ConsoleColor ForegroundColor)
{
_foregroundCol = Console.ForegroundColor;
Console.ForegroundColor = ForegroundColor;
_backgroundCol = Console.BackgroundColor;
}
/// <summary>
/// Initializes a new instance of the <see cref="ConsoleColorScope"/> class.
/// </summary>
/// <param name="ForegroundColor">Color of the foreground.</param>
/// <param name="BackgroundColor">Color of the background.</param>
public ConsoleColorScope(ConsoleColor ForegroundColor, ConsoleColor BackgroundColor)
{
_foregroundCol = Console.ForegroundColor;
Console.ForegroundColor = ForegroundColor;
_backgroundCol = Console.BackgroundColor;
Console.BackgroundColor = BackgroundColor;
}
#region IDisposable Members
///<summary>
///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///</summary>
///<filterpriority>2</filterpriority>
public void Dispose()
{
Console.BackgroundColor = _backgroundCol;
Console.ForegroundColor = _foregroundCol;
}
#endregion
}
}