From b2ad000e07e1c87d3561b5aa94fdc88c779872f0 Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期二, 18 二月 2025 22:34:54 +0800
Subject: [PATCH] 1

---
 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/Helper/FileHelper.cs |  451 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 451 insertions(+), 0 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Helper/FileHelper.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Helper/FileHelper.cs"
new file mode 100644
index 0000000..df88f8a
--- /dev/null
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Helper/FileHelper.cs"
@@ -0,0 +1,451 @@
+锘縰sing 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 鍙栧緱鏂囦欢鍚庣紑鍚�
+        /****************************************
+          * 鍑芥暟鍚嶇О锛欸etPostfixStr
+          * 鍔熻兘璇存槑锛氬彇寰楁枃浠跺悗缂�鍚�
+          * 鍙�     鏁帮細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 鍐欐枃浠�
+        /****************************************
+          * 鍑芥暟鍚嶇О锛歐riteFile
+          * 鍔熻兘璇存槑锛氬啓鏂囦欢,浼氳鐩栨帀浠ュ墠鐨勫唴瀹�
+          * 鍙�     鏁帮細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 璇绘枃浠�
+        /****************************************
+          * 鍑芥暟鍚嶇О锛歊eadFile
+          * 鍔熻兘璇存槑锛氳鍙栨枃鏈唴瀹�
+          * 鍙�     鏁帮細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 杩藉姞鏂囦欢
+        /****************************************
+          * 鍑芥暟鍚嶇О锛欶ileAdd
+          * 鍔熻兘璇存槑锛氳拷鍔犳枃浠跺唴瀹�
+          * 鍙�     鏁帮細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 鎷疯礉鏂囦欢
+        /****************************************
+          * 鍑芥暟鍚嶇О锛欶ileCoppy
+          * 鍔熻兘璇存槑锛氭嫹璐濇枃浠�
+          * 鍙�     鏁帮細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 鍒犻櫎鏂囦欢
+        /****************************************
+          * 鍑芥暟鍚嶇О锛欶ileDel
+          * 鍔熻兘璇存槑锛氬垹闄ゆ枃浠�
+          * 鍙�     鏁帮細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 绉诲姩鏂囦欢
+        /****************************************
+          * 鍑芥暟鍚嶇О锛欶ileMove
+          * 鍔熻兘璇存槑锛氱Щ鍔ㄦ枃浠�
+          * 鍙�     鏁帮細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 鍦ㄥ綋鍓嶇洰褰曚笅鍒涘缓鐩綍
+        /****************************************
+          * 鍑芥暟鍚嶇О锛欶olderCreate
+          * 鍔熻兘璇存槑锛氬湪褰撳墠鐩綍涓嬪垱寤虹洰褰�
+          * 鍙�     鏁帮細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 閫掑綊鍒犻櫎鏂囦欢澶圭洰褰曞強鏂囦欢
+        /****************************************
+          * 鍑芥暟鍚嶇О锛欴eleteFolder
+          * 鍔熻兘璇存槑锛氶�掑綊鍒犻櫎鏂囦欢澶圭洰褰曞強鏂囦欢
+          * 鍙�     鏁帮細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 灏嗘寚瀹氭枃浠跺す涓嬮潰鐨勬墍鏈夊唴瀹筩opy鍒扮洰鏍囨枃浠跺す涓嬮潰 鏋滅洰鏍囨枃浠跺す涓哄彧璇诲睘鎬у氨浼氭姤閿欍��
+        /****************************************
+          * 鍑芥暟鍚嶇О锛欳opyDir
+          * 鍔熻兘璇存槑锛氬皢鎸囧畾鏂囦欢澶逛笅闈㈢殑鎵�鏈夊唴瀹筩opy鍒扮洰鏍囨枃浠跺す涓嬮潰 鏋滅洰鏍囨枃浠跺す涓哄彧璇诲睘鎬у氨浼氭姤閿欍��
+          * 鍙�     鏁帮細srcPath:鍘熷璺緞,aimPath:鐩爣鏂囦欢澶�
+          * 璋冪敤绀哄垪锛�
+          *            string srcPath = Server.MapPath("test/");  
+          *            string aimPath = Server.MapPath("test1/");
+          *            EC.FileObj.CopyDir(srcPath,aimPath);   
+         *****************************************/
+        /// <summary>
+        /// 鎸囧畾鏂囦欢澶逛笅闈㈢殑鎵�鏈夊唴瀹筩opy鍒扮洰鏍囨枃浠跺す涓嬮潰
+        /// </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);
+                // 寰楀埌婧愮洰褰曠殑鏂囦欢鍒楄〃锛岃閲岄潰鏄寘鍚枃浠朵互鍙婄洰褰曡矾寰勭殑涓�涓暟缁�
+                //濡傛灉浣犳寚鍚慶opy鐩爣鏂囦欢涓嬮潰鐨勬枃浠惰�屼笉鍖呭惈鐩綍璇蜂娇鐢ㄤ笅闈㈢殑鏂规硶
+                //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
+    }
+}

--
Gitblit v1.9.3