發(fā)表日期:2015-09-05 文章編輯:南昌開優(yōu)網(wǎng)絡(luò) 瀏覽次數(shù):3845 標(biāo)簽:ASP.NET應(yīng)用
/********************************************** * 類作用: 文件實(shí)用類 * 建立人: 開優(yōu)網(wǎng)絡(luò) ***********************************************/ using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Web; namespace Svnhost.Common { /// <summary> /// 文件實(shí)用類 /// </summary> public sealed class FileUtil { private FileUtil() { } /// <summary> /// 創(chuàng)建目錄 /// </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">目標(biāo)文件名</param> /// <param name="overwrite">當(dāng)目標(biāo)文件存在時是否覆蓋</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> /// 備份文件,當(dāng)目標(biāo)文件存在時覆蓋 /// </summary> /// <param name="sourceFileName">源文件名</param> /// <param name="destFileName">目標(biāo)文件名</param> /// <returns>操作是否成功</returns> public static bool BackupFile(string sourceFileName, string destFileName) { return BackupFile(sourceFileName, destFileName, true); } /// <summary> /// 恢復(fù)文件 /// </summary> /// <param name="backupFileName">備份文件名</param> /// <param name="targetFileName">要恢復(fù)的文件名</param> /// <param name="backupTargetFileName">要恢復(fù)文件再次備份的名稱,如果為null,則不再備份恢復(fù)文件</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); } } }