¶Ô±ÈÐÂÎļþ |
| | |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | |
| | | namespace WIDESEA_Core.Helper |
| | | { |
| | | public class FileHelper : IDisposable |
| | | { |
| | | |
| | | private bool _alreadyDispose = false; |
| | | |
| | | #region æé 彿° |
| | | public FileHelper() |
| | | { |
| | | |
| | | } |
| | | ~FileHelper() |
| | | { |
| | | Dispose(); ; |
| | | } |
| | | |
| | | protected virtual void Dispose(bool isDisposing) |
| | | { |
| | | if (_alreadyDispose) return; |
| | | _alreadyDispose = true; |
| | | } |
| | | #endregion |
| | | |
| | | #region IDisposable æå |
| | | |
| | | public void Dispose() |
| | | { |
| | | Dispose(true); |
| | | GC.SuppressFinalize(this); |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region å徿件åç¼å |
| | | /**************************************** |
| | | * 彿°åç§°ï¼GetPostfixStr |
| | | * åè½è¯´æï¼å徿件åç¼å |
| | | * å æ°ï¼filename:æä»¶åç§° |
| | | * è°ç¨ç¤ºåï¼ |
| | | * string filename = "aaa.aspx"; |
| | | * string s = EC.FileObj.GetPostfixStr(filename); |
| | | *****************************************/ |
| | | /// <summary> |
| | | /// ååç¼å |
| | | /// </summary> |
| | | /// <param name="filename">æä»¶å</param> |
| | | /// <returns>.gif|.htmlæ ¼å¼</returns> |
| | | public static string GetPostfixStr(string filename) |
| | | { |
| | | int start = filename.LastIndexOf("."); |
| | | int length = filename.Length; |
| | | string postfix = filename.Substring(start, length - start); |
| | | return postfix; |
| | | } |
| | | #endregion |
| | | |
| | | #region æ ¹æ®æä»¶å¤§å°è·åæå®åç¼çå¯ç¨æä»¶å |
| | | /// <summary> |
| | | /// æ ¹æ®æä»¶å¤§å°è·åæå®åç¼çå¯ç¨æä»¶å |
| | | /// </summary> |
| | | /// <param name="folderPath">æä»¶å¤¹</param> |
| | | /// <param name="prefix">æä»¶åç¼</param> |
| | | /// <param name="size">æä»¶å¤§å°(1m)</param> |
| | | /// <param name="ext">æä»¶åç¼(.log)</param> |
| | | /// <returns>å¯ç¨æä»¶å</returns> |
| | | public static string GetAvailableFileWithPrefixOrderSize(string folderPath, string prefix, int size = 1 * 1024 * 1024, string ext = ".log") |
| | | { |
| | | var allFiles = new DirectoryInfo(folderPath); |
| | | var selectFiles = allFiles.GetFiles().Where(fi => fi.Name.ToLower().Contains(prefix.ToLower()) && fi.Extension.ToLower() == ext.ToLower() && fi.Length < size).OrderByDescending(d => d.Name).ToList(); |
| | | |
| | | if (selectFiles.Count > 0) |
| | | { |
| | | return selectFiles.FirstOrDefault().FullName; |
| | | } |
| | | |
| | | return Path.Combine(folderPath, $@"{prefix}_{DateTime.Now.DateToTimeStamp()}.log"); |
| | | } |
| | | public static string GetAvailableFileNameWithPrefixOrderSize(string _contentRoot, string prefix, int size = 1 * 1024 * 1024, string ext = ".log") |
| | | { |
| | | var folderPath = Path.Combine(_contentRoot, "Log"); |
| | | if (!Directory.Exists(folderPath)) |
| | | { |
| | | Directory.CreateDirectory(folderPath); |
| | | } |
| | | |
| | | var allFiles = new DirectoryInfo(folderPath); |
| | | var selectFiles = allFiles.GetFiles().Where(fi => fi.Name.ToLower().Contains(prefix.ToLower()) && fi.Extension.ToLower() == ext.ToLower() && fi.Length < size).OrderByDescending(d => d.Name).ToList(); |
| | | |
| | | if (selectFiles.Count > 0) |
| | | { |
| | | return selectFiles.FirstOrDefault().Name.Replace(".log", ""); |
| | | } |
| | | |
| | | return $@"{prefix}_{DateTime.Now.DateToTimeStamp()}"; |
| | | } |
| | | #endregion |
| | | |
| | | #region åæä»¶ |
| | | /**************************************** |
| | | * 彿°åç§°ï¼WriteFile |
| | | * åè½è¯´æï¼åæä»¶,ä¼è¦çæä»¥åçå
容 |
| | | * å æ°ï¼Path:æä»¶è·¯å¾,Strings:ææ¬å
容 |
| | | * è°ç¨ç¤ºåï¼ |
| | | * string Path = Server.MapPath("Default2.aspx"); |
| | | * string Strings = "è¿æ¯æåçå
容å"; |
| | | * EC.FileObj.WriteFile(Path,Strings); |
| | | *****************************************/ |
| | | /// <summary> |
| | | /// åæä»¶ |
| | | /// </summary> |
| | | /// <param name="Path">æä»¶è·¯å¾</param> |
| | | /// <param name="Strings">æä»¶å
容</param> |
| | | public static void WriteFile(string Path, string Strings) |
| | | { |
| | | if (!File.Exists(Path)) |
| | | { |
| | | FileStream f = File.Create(Path); |
| | | f.Close(); |
| | | } |
| | | StreamWriter f2 = new StreamWriter(Path, false, System.Text.Encoding.UTF8); |
| | | f2.Write(Strings); |
| | | f2.Close(); |
| | | f2.Dispose(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// åæä»¶ |
| | | /// </summary> |
| | | /// <param name="Path">æä»¶è·¯å¾</param> |
| | | /// <param name="Strings">æä»¶å
容</param> |
| | | public static void WriteFileAndDelOldFile(string Path, string Strings) |
| | | { |
| | | if (!File.Exists(Path)) |
| | | { |
| | | FileStream f = File.Create(Path); |
| | | f.Close(); |
| | | } |
| | | else |
| | | { |
| | | File.Delete(Path); |
| | | } |
| | | StreamWriter f2 = new StreamWriter(Path, false, System.Text.Encoding.UTF8); |
| | | f2.Write(Strings); |
| | | f2.Close(); |
| | | f2.Dispose(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// åæä»¶ |
| | | /// </summary> |
| | | /// <param name="Path">æä»¶è·¯å¾</param> |
| | | /// <param name="Strings">æä»¶å
容</param> |
| | | public static void WriteFile(string Path, byte[] buf) |
| | | { |
| | | if (!File.Exists(Path)) |
| | | { |
| | | FileStream f = File.Create(Path); |
| | | f.Close(); |
| | | } |
| | | FileStream f2 = new FileStream(Path, FileMode.Create, FileAccess.Write); |
| | | f2.Write(buf, 0, buf.Length); |
| | | f2.Close(); |
| | | f2.Dispose(); |
| | | } |
| | | |
| | | public static void WriteFile(string Path, string fileName, byte[] buf) |
| | | { |
| | | if (!Directory.Exists(Path)) |
| | | { |
| | | Directory.CreateDirectory(Path); |
| | | } |
| | | if (!File.Exists(Path + "\\" + fileName)) |
| | | { |
| | | FileStream f = File.Create(Path + "\\" + fileName); |
| | | f.Close(); |
| | | } |
| | | FileStream f2 = new FileStream(Path + "\\" + fileName, FileMode.Create, FileAccess.Write); |
| | | f2.Write(buf, 0, buf.Length); |
| | | f2.Close(); |
| | | f2.Dispose(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// åæä»¶ |
| | | /// </summary> |
| | | /// <param name="Path">æä»¶è·¯å¾</param> |
| | | /// <param name="Strings">æä»¶å
容</param> |
| | | /// <param name="encode">ç¼ç æ ¼å¼</param> |
| | | public static void WriteFile(string Path, string Strings, Encoding encode) |
| | | { |
| | | if (!File.Exists(Path)) |
| | | { |
| | | FileStream f = File.Create(Path); |
| | | f.Close(); |
| | | } |
| | | StreamWriter f2 = new StreamWriter(Path, false, encode); |
| | | f2.Write(Strings); |
| | | f2.Close(); |
| | | f2.Dispose(); |
| | | } |
| | | #endregion |
| | | |
| | | #region 读æä»¶ |
| | | /**************************************** |
| | | * 彿°åç§°ï¼ReadFile |
| | | * åè½è¯´æï¼è¯»åææ¬å
容 |
| | | * å æ°ï¼Path:æä»¶è·¯å¾ |
| | | * è°ç¨ç¤ºåï¼ |
| | | * string Path = Server.MapPath("Default2.aspx"); |
| | | * string s = EC.FileObj.ReadFile(Path); |
| | | *****************************************/ |
| | | /// <summary> |
| | | /// 读æä»¶ |
| | | /// </summary> |
| | | /// <param name="Path">æä»¶è·¯å¾</param> |
| | | /// <returns></returns> |
| | | public static string ReadFile(string Path) |
| | | { |
| | | string s = ""; |
| | | if (!File.Exists(Path)) |
| | | s = "ä¸åå¨ç¸åºçç®å½"; |
| | | else |
| | | { |
| | | StreamReader f2 = new StreamReader(Path, System.Text.Encoding.GetEncoding("gb2312")); |
| | | s = f2.ReadToEnd(); |
| | | f2.Close(); |
| | | f2.Dispose(); |
| | | } |
| | | |
| | | return s; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 读æä»¶ |
| | | /// </summary> |
| | | /// <param name="Path">æä»¶è·¯å¾</param> |
| | | /// <param name="encode">ç¼ç æ ¼å¼</param> |
| | | /// <returns></returns> |
| | | public static string ReadFile(string Path, Encoding encode) |
| | | { |
| | | string s = ""; |
| | | if (!File.Exists(Path)) |
| | | s = "ä¸åå¨ç¸åºçç®å½"; |
| | | else |
| | | { |
| | | StreamReader f2 = new StreamReader(Path, encode); |
| | | s = f2.ReadToEnd(); |
| | | f2.Close(); |
| | | f2.Dispose(); |
| | | } |
| | | |
| | | return s; |
| | | } |
| | | #endregion |
| | | |
| | | #region è¿½å æä»¶ |
| | | /**************************************** |
| | | * 彿°åç§°ï¼FileAdd |
| | | * åè½è¯´æï¼è¿½å æä»¶å
容 |
| | | * å æ°ï¼Path:æä»¶è·¯å¾,strings:å
容 |
| | | * è°ç¨ç¤ºåï¼ |
| | | * string Path = Server.MapPath("Default2.aspx"); |
| | | * string Strings = "æ°è¿½å å
容"; |
| | | * EC.FileObj.FileAdd(Path, Strings); |
| | | *****************************************/ |
| | | /// <summary> |
| | | /// è¿½å æä»¶ |
| | | /// </summary> |
| | | /// <param name="Path">æä»¶è·¯å¾</param> |
| | | /// <param name="strings">å
容</param> |
| | | public static void FileAdd(string Path, string strings) |
| | | { |
| | | StreamWriter sw = File.AppendText(Path); |
| | | sw.Write(strings); |
| | | sw.Flush(); |
| | | sw.Close(); |
| | | } |
| | | #endregion |
| | | |
| | | #region æ·è´æä»¶ |
| | | /**************************************** |
| | | * 彿°åç§°ï¼FileCoppy |
| | | * åè½è¯´æï¼æ·è´æä»¶ |
| | | * å æ°ï¼OrignFile:åå§æä»¶,NewFile:æ°æä»¶è·¯å¾ |
| | | * è°ç¨ç¤ºåï¼ |
| | | * string orignFile = Server.MapPath("Default2.aspx"); |
| | | * string NewFile = Server.MapPath("Default3.aspx"); |
| | | * EC.FileObj.FileCoppy(OrignFile, NewFile); |
| | | *****************************************/ |
| | | /// <summary> |
| | | /// æ·è´æä»¶ |
| | | /// </summary> |
| | | /// <param name="OrignFile">åå§æä»¶</param> |
| | | /// <param name="NewFile">æ°æä»¶è·¯å¾</param> |
| | | public static void FileCoppy(string orignFile, string NewFile) |
| | | { |
| | | File.Copy(orignFile, NewFile, true); |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region å 餿件 |
| | | /**************************************** |
| | | * 彿°åç§°ï¼FileDel |
| | | * åè½è¯´æï¼å 餿件 |
| | | * å æ°ï¼Path:æä»¶è·¯å¾ |
| | | * è°ç¨ç¤ºåï¼ |
| | | * string Path = Server.MapPath("Default3.aspx"); |
| | | * EC.FileObj.FileDel(Path); |
| | | *****************************************/ |
| | | /// <summary> |
| | | /// å 餿件 |
| | | /// </summary> |
| | | /// <param name="Path">è·¯å¾</param> |
| | | public static void FileDel(string Path) |
| | | { |
| | | File.Delete(Path); |
| | | } |
| | | #endregion |
| | | |
| | | #region ç§»å¨æä»¶ |
| | | /**************************************** |
| | | * 彿°åç§°ï¼FileMove |
| | | * åè½è¯´æï¼ç§»å¨æä»¶ |
| | | * å æ°ï¼OrignFile:åå§è·¯å¾,NewFile:æ°æä»¶è·¯å¾ |
| | | * è°ç¨ç¤ºåï¼ |
| | | * string orignFile = Server.MapPath("../说æ.txt"); |
| | | * string NewFile = Server.MapPath("http://www.cnblogs.com/说æ.txt"); |
| | | * EC.FileObj.FileMove(OrignFile, NewFile); |
| | | *****************************************/ |
| | | /// <summary> |
| | | /// ç§»å¨æä»¶ |
| | | /// </summary> |
| | | /// <param name="OrignFile">åå§è·¯å¾</param> |
| | | /// <param name="NewFile">æ°è·¯å¾</param> |
| | | public static void FileMove(string orignFile, string NewFile) |
| | | { |
| | | File.Move(orignFile, NewFile); |
| | | } |
| | | #endregion |
| | | |
| | | #region å¨å½åç®å½ä¸å建ç®å½ |
| | | /**************************************** |
| | | * 彿°åç§°ï¼FolderCreate |
| | | * åè½è¯´æï¼å¨å½åç®å½ä¸å建ç®å½ |
| | | * å æ°ï¼OrignFolder:å½åç®å½,NewFloder:æ°ç®å½ |
| | | * è°ç¨ç¤ºåï¼ |
| | | * string orignFolder = Server.MapPath("test/"); |
| | | * string NewFloder = "new"; |
| | | * EC.FileObj.FolderCreate(OrignFolder, NewFloder); |
| | | *****************************************/ |
| | | /// <summary> |
| | | /// å¨å½åç®å½ä¸å建ç®å½ |
| | | /// </summary> |
| | | /// <param name="OrignFolder">å½åç®å½</param> |
| | | /// <param name="NewFloder">æ°ç®å½</param> |
| | | public static void FolderCreate(string orignFolder, string NewFloder) |
| | | { |
| | | Directory.SetCurrentDirectory(orignFolder); |
| | | Directory.CreateDirectory(NewFloder); |
| | | } |
| | | #endregion |
| | | |
| | | #region éå½å 餿件夹ç®å½åæä»¶ |
| | | /**************************************** |
| | | * 彿°åç§°ï¼DeleteFolder |
| | | * åè½è¯´æï¼éå½å 餿件夹ç®å½åæä»¶ |
| | | * å æ°ï¼dir:æä»¶å¤¹è·¯å¾ |
| | | * è°ç¨ç¤ºåï¼ |
| | | * string dir = Server.MapPath("test/"); |
| | | * EC.FileObj.DeleteFolder(dir); |
| | | *****************************************/ |
| | | /// <summary> |
| | | /// éå½å 餿件夹ç®å½åæä»¶ |
| | | /// </summary> |
| | | /// <param name="dir"></param> |
| | | /// <returns></returns> |
| | | public static void DeleteFolder(string dir) |
| | | { |
| | | if (Directory.Exists(dir)) //妿åå¨è¿ä¸ªæä»¶å¤¹å é¤ä¹ |
| | | { |
| | | foreach (string d in Directory.GetFileSystemEntries(dir)) |
| | | { |
| | | if (File.Exists(d)) |
| | | File.Delete(d); //ç´æ¥å é¤å
¶ä¸çæä»¶ |
| | | else |
| | | DeleteFolder(d); //éå½å é¤åæä»¶å¤¹ |
| | | } |
| | | Directory.Delete(dir); //å é¤å·²ç©ºæä»¶å¤¹ |
| | | } |
| | | |
| | | } |
| | | #endregion |
| | | |
| | | #region 尿宿件夹ä¸é¢çææå
容copyå°ç®æ æä»¶å¤¹ä¸é¢ æç®æ æä»¶å¤¹ä¸ºåªè¯»å±æ§å°±ä¼æ¥éã |
| | | /**************************************** |
| | | * 彿°åç§°ï¼CopyDir |
| | | * åè½è¯´æï¼å°æå®æä»¶å¤¹ä¸é¢çææå
容copyå°ç®æ æä»¶å¤¹ä¸é¢ æç®æ æä»¶å¤¹ä¸ºåªè¯»å±æ§å°±ä¼æ¥éã |
| | | * å æ°ï¼srcPath:åå§è·¯å¾,aimPath:ç®æ æä»¶å¤¹ |
| | | * è°ç¨ç¤ºåï¼ |
| | | * string srcPath = Server.MapPath("test/"); |
| | | * string aimPath = Server.MapPath("test1/"); |
| | | * EC.FileObj.CopyDir(srcPath,aimPath); |
| | | *****************************************/ |
| | | /// <summary> |
| | | /// æå®æä»¶å¤¹ä¸é¢çææå
容copyå°ç®æ æä»¶å¤¹ä¸é¢ |
| | | /// </summary> |
| | | /// <param name="srcPath">åå§è·¯å¾</param> |
| | | /// <param name="aimPath">ç®æ æä»¶å¤¹</param> |
| | | public static void CopyDir(string srcPath, string aimPath) |
| | | { |
| | | try |
| | | { |
| | | // æ£æ¥ç®æ ç®å½æ¯å¦ä»¥ç®å½åå²åç¬¦ç»æå¦æä¸æ¯åæ·»å ä¹ |
| | | if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar) |
| | | aimPath += Path.DirectorySeparatorChar; |
| | | // å¤æç®æ ç®å½æ¯å¦åå¨å¦æä¸åå¨åæ°å»ºä¹ |
| | | if (!Directory.Exists(aimPath)) |
| | | Directory.CreateDirectory(aimPath); |
| | | // å¾å°æºç®å½çæä»¶å表ï¼è¯¥é颿¯å
嫿件以åç®å½è·¯å¾çä¸ä¸ªæ°ç» |
| | | //å¦æä½ æåcopyç®æ æä»¶ä¸é¢çæä»¶èä¸å
å«ç®å½è¯·ä½¿ç¨ä¸é¢çæ¹æ³ |
| | | //string[] fileList = Directory.GetFiles(srcPath); |
| | | string[] fileList = Directory.GetFileSystemEntries(srcPath); |
| | | //éåææçæä»¶åç®å½ |
| | | foreach (string file in fileList) |
| | | { |
| | | //å
å½ä½ç®å½å¤ç妿åå¨è¿ä¸ªç®å½å°±éå½Copy该ç®å½ä¸é¢çæä»¶ |
| | | |
| | | if (Directory.Exists(file)) |
| | | CopyDir(file, aimPath + Path.GetFileName(file)); |
| | | //å¦åç´æ¥Copyæä»¶ |
| | | else |
| | | File.Copy(file, aimPath + Path.GetFileName(file), true); |
| | | } |
| | | |
| | | } |
| | | catch (Exception ee) |
| | | { |
| | | throw new Exception(ee.ToString()); |
| | | } |
| | | } |
| | | #endregion |
| | | } |
| | | } |