CSharpUtils.ConsoleUtils.CaptureOutput C# (CSharp) Method

CaptureOutput() public static method

public static CaptureOutput ( System.Action Action, bool Capture = true ) : String
Action System.Action
Capture bool
return String
		public static String CaptureOutput(Action Action, bool Capture = true)
		{
			if (Capture)
			{
				var OldOut = Console.Out;
				var StringWriter = new StringWriter();
				try
				{
					Console.SetOut(StringWriter);
					Action();
				}
				finally
				{
					Console.SetOut(OldOut);
				}
				try
				{
					return StringWriter.ToString();
				}
				catch
				{
					return "";
				}
			}
			else
			{
				Action();
				return "";
			}
		}
	}