/********************************************** * 类作用: 文件实用类 * 建立人: 开优网络 ***********************************************/ using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Web; namespace Svnhost.Common { /// <summary> /// 文件实用类 /// </summary> public sealed class FileUtil { private FileUtil() { } /// <summary> /// 创建目录 /// </summary> /// <param name="FileOrPath">文件或目录</param> public static void CreateDirectory(string FileOrPath) { if (FileOrPath != null) { string path; if (FileOrPath.Contains(".")) path = Path.GetDirectoryName(FileOrPath); else path = FileOrPath; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } } /// <summary> /// 读取文件 /// </summary> /// <param name="filename"></param> /// <param name="encoding"></param> /// <param name="isCache"></param> /// <returns></returns> public static string ReadFile(string filename, Encoding encoding, bool isCache) { string body; if (isCache) { body = (string)HttpContext.Current.Cache[filename]; if (body == null) { body = ReadFile(filename, encoding, false); HttpContext.Current.Cache.Add(filename, body, new System.Web.Caching.CacheDependency(filename), DateTime.MaxValue, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null); } } else { using (StreamReader sr = new StreamReader(filename, encoding)) { body = sr.ReadToEnd(); } } return body; } /// <summary> /// 备份文件 /// </summary> /// <param name="sourceFileName">源文件名</param> /// <param name="destFileName">目标文件名</param> /// <param name="overwrite">当目标文件存在时是否覆盖</param> /// <returns>操作是否成功</returns> public static bool BackupFile(string sourceFileName, string destFileName, bool overwrite) { if (!System.IO.File.Exists(sourceFileName)) { throw new FileNotFoundException(sourceFileName + "文件不存在!"); } if (!overwrite && System.IO.File.Exists(destFileName)) { return false; } try { System.IO.File.Copy(sourceFileName, destFileName, true); return true; } catch (Exception e) { throw e; } } /// <summary> /// 备份文件,当目标文件存在时覆盖 /// </summary> /// <param name="sourceFileName">源文件名</param> /// <param name="destFileName">目标文件名</param> /// <returns>操作是否成功</returns> public static bool BackupFile(string sourceFileName, string destFileName) { return BackupFile(sourceFileName, destFileName, true); } /// <summary> /// 恢复文件 /// </summary> /// <param name="backupFileName">备份文件名</param> /// <param name="targetFileName">要恢复的文件名</param> /// <param name="backupTargetFileName">要恢复文件再次备份的名称,如果为null,则不再备份恢复文件</param> /// <returns>操作是否成功</returns> public static bool RestoreFile(string backupFileName, string targetFileName, string backupTargetFileName) { try { if (!System.IO.File.Exists(backupFileName)) { throw new FileNotFoundException(backupFileName + "文件不存在!"); } if (backupTargetFileName != null) { if (!System.IO.File.Exists(targetFileName)) { throw new FileNotFoundException(targetFileName + "文件不存在!无法备份此文件!"); } else { System.IO.File.Copy(targetFileName, backupTargetFileName, true); } } System.IO.File.Delete(targetFileName); System.IO.File.Copy(backupFileName, targetFileName); } catch (Exception e) { throw e; } return true; } public static bool RestoreFile(string backupFileName, string targetFileName) { return RestoreFile(backupFileName, targetFileName, null); } } }