using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Yaulw.File
{
///
/// Simple Line-By-Line UTF/Ascii File Writer Object
///
public class FileWriter
{
#region Private Members
private string _FileName = "";
private string _FileType = "";
private string _dirPath = "";
private bool _OverideExisting = false;
private bool _FileIsCreated = false;
#endregion
#region Construction
///
/// Create a Simple UTF/Ascii Line FileWriter Object
///
/// The Name of the File (If blank will generate a random file name)
/// The Type of File to write to (Default = "log")
/// The path where to write the file (If Blank will use Temp Path)
/// true to overide an existing file, false will try to append by default
public FileWriter(string FileName = "", string FileType = "log", string dirPath = "", bool OverideExisting = true)
{
_FileName = FileName;
_FileType = FileType;
_dirPath = dirPath;
_OverideExisting = OverideExisting;
// Generate File Name and Path, if not exist
if(String.IsNullOrEmpty(_FileName))
_FileName = System.IO.Path.GetFileNameWithoutExtension(System.IO.Path.GetRandomFileName());
if(String.IsNullOrEmpty(_dirPath))
_dirPath = System.IO.Path.GetTempPath();
// Make Sure Path Exists
if (!Directory.Exists(_dirPath))
Directory.CreateDirectory(_dirPath);
}
#endregion
#region Write Ascii
///
/// Write a Line in Ascii to File
///
/// ascii line to write
public void WriteLineA(string line)
{
using (FileStream fs = CreateFileStream())
using(StreamWriter sw = new StreamWriter(fs, Encoding.ASCII))
{
sw.WriteLine(line);
sw.Flush();
}
}
///
/// Write Lines in Ascii to file
///
/// ascii lines to write
public void WriteLineA(string[] lines)
{
using (FileStream fs = CreateFileStream())
using (StreamWriter sw = new StreamWriter(fs, Encoding.ASCII))
{
foreach (string line in lines)
sw.WriteLine(line);
sw.Flush();
}
}
#endregion
#region Write UTF
///
/// Write a Line in UTF to File
///
/// utf line to write
public void WriteLineUTF8(string line)
{
using (FileStream fs = CreateFileStream())
using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
{
sw.WriteLine(line);
sw.Flush();
}
}
///
/// Write Lines in UTF to File
///
/// utf lines to write
public void WriteLineUTF8(string[] lines)
{
using (FileStream fs = CreateFileStream())
using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
{
foreach (string line in lines)
sw.WriteLine(line);
sw.Flush();
}
}
#endregion
#region Public Properties
///
/// Returns the File Name
///
public string FileName { get { return (this._FileName + "." + this._FileType); } }
///
/// Returns the Path where the File is located
///
public string Path { get { return (this._dirPath); } }
///
/// Returns both Path and FileName
///
public string FileNameNPath { get { return (this.Path + "\\" + this.FileName); } }
#endregion
#region Public Methods
///
/// Deletes the File, if it exists
///
/// true if successful, false otherwise
public bool DeleteFile()
{
try
{
if (System.IO.File.Exists(this.FileNameNPath))
{
System.IO.File.Delete(this.FileNameNPath);
_FileIsCreated = false;
return true;
}
}
catch (Exception) { /* ignore */ }
return false;
}
#endregion
#region Private Methods
///
/// Creates the File Stream, either in Create or Append Mode
///
/// a File Stream to Write to * Must be Closed by Caller *
private FileStream CreateFileStream()
{
try
{
bool bFileExists = System.IO.File.Exists(this.FileNameNPath);
if (!_FileIsCreated && _OverideExisting && bFileExists)
{
_FileIsCreated = true;
return (new FileStream(this.FileNameNPath, FileMode.Create));
}
else if (bFileExists)
{
return (new FileStream(this.FileNameNPath, FileMode.Append));
}
else
{
_FileIsCreated = true;
return (new FileStream(this.FileNameNPath, FileMode.Create));
}
}
catch (Exception) { /* ignore */ }
return null;
}
#endregion
}
}