using System;
using System.IO;
using System.Xml;
namespace Wayne.Lib.IO
{
///
/// Support class for files.
///
public static class FileSupport
{
#region Static fields
///
/// Default encoding for reading and writing text files.
///
public static readonly System.Text.Encoding DefaultEncoding = System.Text.Encoding.UTF8;
///
/// Static instance of FileSupport.
///
public static IFileSupport fileSupport = new FileSupportNonStatic();
///
/// Platform-dependent extension of file support.
///
public static IFileSupportExtension FileSupportExtension
{
get { return fileSupport.FileSupportExtension; }
set { fileSupport.FileSupportExtension = value; }
}
///
/// Secure delete support extension. Dependent on the platform.
///
public static ISecureDeleteSupport SecureDeleteSupport
{
get { return fileSupport.SecureDeleteSupport; }
set { fileSupport.SecureDeleteSupport = value; }
}
#endregion
#region Open
///
/// Opens a file.
///
///
///
///
///
///
///
///
public static Stream Open(string fileName, FileMode fileMode, FileAccess fileAccess, FileShare fileShare, int retries, int delayBetweenRetries)
{
return fileSupport.Open(fileName, fileMode, fileAccess, fileShare, retries, delayBetweenRetries);
}
///
/// Opens a file, using standard values for retries (100) and delayBetweenRetries(100)
///
///
///
///
///
///
public static Stream Open(string fileName, FileMode fileMode, FileAccess fileAccess, FileShare fileShare)
{
return fileSupport.Open(fileName, fileMode, fileAccess, fileShare);
}
#endregion
#region Move
///
/// Moves a file.
///
///
///
///
///
public static void Move(string sourceFileName, string destinationFileName, int retries, int delayBetweenRetries)
{
fileSupport.Move(sourceFileName, destinationFileName, retries, delayBetweenRetries);
}
///
/// Moves a file, using standard values for retries (100) and delayBetweenRetries(100)
///
///
///
public static void Move(string sourceFileName, string destinationFileName)
{
fileSupport.Move(sourceFileName, destinationFileName);
}
#endregion
#region Delete
///
/// Deletes a file
///
///
///
///
public static void Delete(string fileName, int retries, int delayBetweenRetries)
{
fileSupport.Delete(fileName, retries, delayBetweenRetries);
}
///
/// Deletes a file, using standard values for retries (100) and delayBetweenRetries(100)
///
///
[System.Diagnostics.DebuggerStepThrough]
public static void Delete(string fileName)
{
fileSupport.Delete(fileName);
}
#endregion
#region SecureDelete
///
/// Deletes a file securely.
///
///
///
///
///
public static void SecureDelete(string fileName, out bool sDeleteOK, int retries, int delayBetweenRetries)
{
fileSupport.SecureDelete(fileName, out sDeleteOK, retries, delayBetweenRetries);
}
///
/// Deletes a file securely.
///
///
///
public static void SecureDelete(string fileName, out bool sDeleteOK)
{
fileSupport.SecureDelete(fileName, out sDeleteOK);
}
#endregion
#region Copy
///
/// Copy a file.
///
///
///
///
public static void Copy(string sourceFileName, string destinationFileName, bool overwrite)
{
fileSupport.Copy(sourceFileName, destinationFileName, overwrite);
}
///
/// Copies a file.
///
///
///
///
///
///
public static void Copy(string sourceFileName, string destinationFileName, bool overwrite, int retries, int delayBetweenRetries)
{
fileSupport.Copy(sourceFileName, destinationFileName, overwrite, retries, delayBetweenRetries);
}
#endregion
#region LoadFromFile/SaveToFile
///
/// Read the lines of a text file into a string.
///
/// The path and file name.
public static string LoadToString(string fileName)
{
return fileSupport.LoadToString(fileName, DefaultEncoding);
}
///
/// Read the lines of a text file into a string.
///
/// The path and file name.
/// The encoding.
public static string LoadToString(string fileName, System.Text.Encoding encoding)
{
return fileSupport.LoadToString(fileName, encoding);
}
///
/// Read the lines of a text file into an array of strings.
///
/// The path and file name.
public static string[] LoadToStringArray(string fileName)
{
return fileSupport.LoadToStringArray(fileName);
}
///
/// Read the lines of a text file into an array of strings.
///
/// The path and file name.
/// The encoding.
public static string[] LoadToStringArray(string fileName, System.Text.Encoding encoding)
{
return fileSupport.LoadToStringArray(fileName, encoding);
}
///
/// Saves a text string to a file.
///
/// The path and file name.
/// The text to save.
public static void SaveToFile(string fileName, string text)
{
fileSupport.SaveToFile(fileName, text);
}
///
/// Saves a text string to a file.
///
/// The path and file name.
/// The lines to save.
public static void SaveToFile(string fileName, string[] lines)
{
fileSupport.SaveToFile(fileName, lines);
}
///
/// Saves a text string to a file.
///
/// The path and file name.
/// The text to save.
/// The encoding.
public static void SaveToFile(string fileName, string text, System.Text.Encoding encoding)
{
fileSupport.SaveToFile(fileName, text, encoding);
}
///
/// Saves a text string to a file.
///
/// The path and file name.
/// The lines to save.
/// The encoding.
public static void SaveToFile(string fileName, string[] lines, System.Text.Encoding encoding)
{
fileSupport.SaveToFile(fileName, lines, encoding);
}
///
/// Loads the specified XML file into the XmlDocument.
///
/// The XmlDocument to load.
/// The path and file name.
public static void LoadXml(XmlDocument xmlDocument, string fileName)
{
fileSupport.LoadXml(xmlDocument, fileName);
}
///
/// Loads the specified XML file into the XmlDocument.
///
/// The XmlDocument to load.
/// The path and file name.
/// The encoding.
public static void LoadXml(XmlDocument xmlDocument, string fileName, System.Text.Encoding encoding)
{
fileSupport.LoadXml(xmlDocument, fileName, encoding);
}
#endregion
///
/// Set creation time on a file.
///
///
public static DateTime GetCreationTime(string fileName)
{
return fileSupport.GetCreationTime(fileName);
}
///
/// Set creation time on a file.
///
///
///
public static void SetCreationTime(string fileName, DateTime dateTime)
{
fileSupport.SetCreationTime(fileName, dateTime);
}
///
/// Get last access time on the file.
///
///
public static DateTime GetLastAccessTime(string fileName)
{
return fileSupport.GetLastAccessTime(fileName);
}
///
/// Set last access time on a file.
///
///
///
public static void SetLastAccessTime(string fileName, DateTime dateTime)
{
fileSupport.SetLastAccessTime(fileName, dateTime);
}
///
/// Get last write time on the file.
///
///
public static DateTime GetLastWriteTime(string fileName)
{
return fileSupport.GetLastWriteTime(fileName);
}
///
/// Set last write time on the file.
///
///
///
public static void SetLastWriteTime(string fileName, DateTime dateTime)
{
fileSupport.SetLastWriteTime(fileName, dateTime);
}
///
/// Checks if the specified file exists.
///
///
///
public static bool FileExists(string fileName)
{
return fileSupport.FileExists(fileName);
}
///
/// Returns the names of files in the specified directory that match the specified search pattern.
///
/// The directory to search.
/// The search string to match against the names of files in path. The parameter cannot end in two periods ("..") or contain two periods ("..") followed by System.IO.Path.DirectorySeparatorChar or System.IO.Path.AltDirectorySeparatorChar, nor can it contain any of the characters in System.IO.Path.InvalidPathChars.
///
public static string[] GetFiles(string path, string searchPattern)
{
return fileSupport.GetFiles(path, searchPattern);
}
///
/// Checks if directory exists
///
///
///
public static bool DirectoryExists(string path)
{
return fileSupport.DirectoryExists(path);
}
///
/// Ensures the directory exists by trying to create it if it does not exist already.
///
///
///
public static bool EnsureDirectoryExists(string path)
{
return fileSupport.EnsureDirectoryExists(path);
}
///
/// Checks if the filename is valid.
///
///
/// True if the filename is valid otherwise false.
public static bool IsValidFileName(string fileName)
{
return fileSupport.IsValidFileName(fileName);
}
///
/// Checks if the filepath is valid
///
///
/// True if the filepath is valid otherwise false.
public static bool IsValidPathName(string pathName)
{
return fileSupport.IsValidPathName(pathName);
}
}
}