using System.IO;
using System.Text;
namespace Chernobyl.TextWriters
{
///
/// This class is used to write text to
/// a file dynamically. More specifically,
/// it was created to redirect console output
/// to a file and ensure that the file was
/// kept up-to-date. This prevents errors from
/// interfering with the results of the output.
///
public class DynamicFileTextWriter : TextWriter
{
///
/// Constructor.
///
/// The FileStream to write
/// the output to.
public DynamicFileTextWriter(FileStream fileStream)
{
File = fileStream;
}
///
/// Writes a line of text out which is
/// ended with a newline.
///
/// The string to write to the file.
public override void WriteLine(string value)
{
// append a newline, write to the file, and force
// the file write.
value += System.Environment.NewLine;
Write(value);
}
///
/// Writes the string to the file.
///
/// The string to write to the file.
public override void Write(string value)
{
File.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(value), 0, value.Length);
File.Flush();
}
///
/// When overridden in a derived class, returns the
/// in which the output is written.
///
/// The encoding in which the output is written.
/// The Encoding in which the output is written.
public override Encoding Encoding
{
get { return Encoding.ASCII; }
}
///
/// The file to write the output to.
///
FileStream File {get; set;}
}
}