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