From 25429f085093d89d543a0b90e30d0d62d1b7dac9 Mon Sep 17 00:00:00 2001 From: hxb <hxb@hdlchina.com.cn> Date: 星期二, 30 八月 2022 09:37:38 +0800 Subject: [PATCH] 合并了IOS的代码 --- ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlFileLogic.cs | 451 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 451 insertions(+), 0 deletions(-) diff --git a/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlFileLogic.cs b/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlFileLogic.cs new file mode 100644 index 0000000..bad19fa --- /dev/null +++ b/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlFileLogic.cs @@ -0,0 +1,451 @@ +锘縰sing Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Shared.Phone.UserCenter +{ + /// <summary> + /// 鏂囦欢鎿嶄綔鐨勯�昏緫 + /// </summary> + public class HdlFileLogic + { + #region 鈻� 鍙橀噺澹版槑___________________________ + + /// <summary> + /// 鏂囦欢鎿嶄綔鐨勯�昏緫 + /// </summary> + private static HdlFileLogic m_Current = null; + /// <summary> + /// 鏂囦欢鎿嶄綔鐨勯�昏緫 + /// </summary> + public static HdlFileLogic Current + { + get + { + if (m_Current == null) + { + m_Current = new HdlFileLogic(); + } + return m_Current; + } + } + #endregion + + #region 鈻� 棰勫垱寤轰釜浜轰腑蹇冨叏閮ㄧ殑鏂囦欢澶筥________ + + /// <summary> + /// 棰勫垱寤轰釜浜轰腑蹇冨叏閮ㄧ殑鏂囦欢澶� + /// </summary> + public void CreatAllUserCenterDirectory() + { + //鏈湴缂撳瓨鐨勬牴鐩綍 + this.CreateDirectory(DirNameResourse.LocalMemoryDirectory); + + //鑷姩澶囦唤銆愭枃浠跺す銆�(缂栬緫,杩藉姞) + this.CreateDirectory(DirNameResourse.AutoBackupDirectory); + + //鑷姩澶囦唤銆愭枃浠跺す銆�(鍒犻櫎) + this.CreateDirectory(DirNameResourse.AutoBackupdeleteDirectory); + + //涓嬭浇澶囦唤鐨勬椂鍊欐墍浣跨敤鐨勪复鏃躲�愭枃浠跺す銆� + this.CreateDirectory(DirNameResourse.DownLoadBackupTempDirectory); + + //淇濆瓨瀹夐槻璁板綍鐨勩�愭枃浠跺す銆� + this.CreateDirectory(DirNameResourse.SafeguardAlarmDirectory); + + //涓嬭浇鍒嗕韩鏂囦欢鐨勪复鏃躲�愭枃浠跺す銆� + this.CreateDirectory(DirNameResourse.DownLoadShardDirectory); + + //LOG鍑哄姏銆愭枃浠跺す銆� + this.CreateDirectory(DirNameResourse.LogDirectory); + + //妯℃澘缂撳瓨瀛樻斁鐨勩�愭枃浠跺す銆� + this.CreateDirectory(DirNameResourse.LocalTemplateDirectory); + this.CreateDirectory(DirNameResourse.AllResidenceTemplateDirectory); + + //鐢ㄦ埛鍥剧墖鐩綍璺緞銆愭枃浠跺す銆� + if (UserCenterResourse.AccountOption.UserPictruePath != string.Empty) + { + this.CreateDirectory(UserCenterResourse.AccountOption.UserPictruePath); + } + } + + #endregion + + #region 鈻� 鏂囦欢淇濆瓨鍜岃鍙朹____________________ + + /// <summary> + /// 鏂囦欢淇濆瓨(鏁村ぉ蹇樿,鎵�浠ュ缓涓�涓嚱鏁版潵鐜╃帺) + /// </summary> + /// <param name="fullName">鍏ㄨ矾寰�</param> + /// <param name="obj">闇�瑕佸簭鍒楀寲鐨勪笢瑗�</param> + public void SaveFileContent(string fullName, object obj) + { + var data = JsonConvert.SerializeObject(obj); + this.SaveTextToFile(fullName, data); + } + + /// <summary> + /// 鏂囦欢淇濆瓨(鏁村ぉ蹇樿,鎵�浠ュ缓涓�涓嚱鏁版潵鐜╃帺) + /// </summary> + /// <param name="fullName">鍏ㄨ矾寰�</param> + /// <param name="textValue">鏂囨湰</param> + public void SaveTextToFile(string fullName, string textValue) + { + var byteData = Encoding.UTF8.GetBytes(textValue); + this.SaveByteToFile(fullName, byteData); + } + + /// <summary> + /// 鏂囦欢淇濆瓨(鏁村ぉ蹇樿,鎵�浠ュ缓涓�涓嚱鏁版潵鐜╃帺) + /// </summary> + /// <param name="fullName">鍏ㄨ矾寰�</param> + /// <param name="byteData">byte鏁版嵁</param> + public void SaveByteToFile(string fullName, byte[] byteData) + { + if (byteData == null) { return; } + //鍐欏叆鍐呭 + System.IO.FileStream fileStream = null; + try + { + fileStream = new System.IO.FileStream(fullName, System.IO.FileMode.Create, System.IO.FileAccess.Write); + fileStream.Write(byteData, 0, byteData.Length); + fileStream.Flush(); + } + catch { } + finally + { + fileStream?.Close(); + } + } + + /// <summary> + /// 璇诲彇鏂囦欢,涓嶉檺鍒朵綇瀹�(鏂囦欢涓嶅瓨鍦ㄨ繑鍥瀗ull,鏁村ぉ蹇樿,鎵�浠ュ缓涓�涓嚱鏁版潵鐜╃帺) + /// </summary> + /// <param name="fullName">鍏ㄨ矾寰�</param> + /// <returns></returns> + public string ReadFileTextContent(string fullName) + { + //璇诲彇鏂囦欢 + var varByte = this.ReadFileByteContent(fullName); + if (varByte == null) + { + return null; + } + return Encoding.UTF8.GetString(varByte); + } + + /// <summary> + /// 璇诲彇鏂囦欢,涓嶉檺鍒朵綇瀹�,鏂囦欢涓嶅瓨鍦ㄨ繑鍥瀗ull + /// </summary> + /// <param name="fullName">鍏ㄨ矾寰�</param> + /// <returns></returns> + public byte[] ReadFileByteContent(string fullName) + { + if (System.IO.File.Exists(fullName) == false) + { + return null; + } + System.IO.FileStream fileStream = null; + try + { + fileStream = new System.IO.FileStream(fullName, System.IO.FileMode.Open, System.IO.FileAccess.Read); + byte[] array = new byte[fileStream.Length]; + fileStream.Read(array, 0, array.Length); + return array; + } + catch + { + return null; + } + finally + { + fileStream?.Close(); + } + } + + #endregion + + #region 鈻� 鏂囦欢澶规搷浣滃嚱鏁癬____________________ + + /// <summary> + /// 鍒涘缓涓�涓枃浠跺す + /// </summary> + /// <param name="fullDirectory">闇�瑕佸垱寤虹殑鏂囦欢澶瑰叏璺緞</param> + /// <param name="clear">濡傛灉鏂囦欢澶瑰瓨鍦紝鏄惁闇�瑕佹竻绌烘枃浠跺す</param> + public void CreateDirectory(string fullDirectory, bool clear = false) + { + if (System.IO.Directory.Exists(fullDirectory) == false) + { + try + { + System.IO.Directory.CreateDirectory(fullDirectory); + } + catch (Exception ex) + { + HdlLogLogic.Current.WriteLog(ex, "鍒涘缓鏂囦欢澶瑰け璐�:" + fullDirectory); + } + } + else if (clear == true) + { + //濡傛灉瀛樺湪锛屽垯娓呯┖鍏ㄩ儴鏂囦欢 + var files = System.IO.Directory.GetFiles(fullDirectory); + foreach (var file in files) + { + this.DeleteFile(file); + } + } + } + + /// <summary> + /// 鍒犻櫎鏂囦欢澶� + /// </summary> + /// <param name="fullDirectory">鏂囦欢澶瑰叏璺緞</param> + public void DeleteDirectory(string fullDirectory) + { + if (System.IO.Directory.Exists(fullDirectory) == true) + { + try { System.IO.Directory.Delete(fullDirectory, true); } + catch (Exception ex) + { + HdlLogLogic.Current.WriteLog(ex, "鍒犻櫎鏂囦欢澶瑰け璐�:" + fullDirectory); + } + } + } + + /// <summary> + /// 鑾峰彇鎸囧畾鏂囦欢澶归噷闈㈢殑鍏ㄩ儴鏂囦欢 + /// </summary> + /// <param name="directory">鏂囦欢璺緞锛堝叏鍚嶏級</param> + /// <param name="onlyFileName">鍗曠函鍙槸鑾峰彇鏂囦欢鍚嶅瓧,濡傛灉涓篺alse鏃�,灏嗚繑鍥炴枃浠剁殑鍏ㄨ矾寰�</param> + /// <returns></returns> + public List<string> GetFileFromDirectory(string directory, bool onlyFileName = true) + { + if (System.IO.Directory.Exists(directory) == false) + { + return new List<string>(); + } + + var list = new List<string>(); + try + { + //2020.07.10鐨勬椂鍊�,鍑虹幇杩囪幏鍙栧け璐ュ紓甯� 鎻愮ず:write fault on path + var files = System.IO.Directory.GetFiles(directory); + foreach (var file in files) + { + string fileName = file; + if (onlyFileName == true) + { + fileName = fileName.Substring(directory.Length + 1); + } + list.Add(fileName); + } + } + catch (Exception ex) + { HdlLogLogic.Current.WriteLog(ex); } + + return list; + } + + /// <summary> + /// 鑾峰彇鏍圭洰褰曠殑鍏ㄩ儴鏂囦欢鍒楄〃 + /// </summary> + /// <returns></returns> + public List<string> GetRootPathListFile() + { + return this.GetFileFromDirectory(Common.Config.Instance.FullPath); + } + + /// <summary> + /// 灏嗘寚瀹氭枃浠跺す閲岄潰鐨勫叏閮ㄦ枃浠剁Щ鍔ㄥ埌褰撳墠浣忓畢鐨勬枃浠跺す鍐� + /// </summary> + /// <param name="fulldirectory">鏂囦欢澶瑰叏璺緞</param> + /// <param name="deleteDirectory">澶勭悊瀹屼箣鍚庯紝鏄惁鎶婃枃浠跺す鍒犻櫎</param> + public void MoveDirectoryFileToHomeDirectory(string fulldirectory, bool deleteDirectory = false) + { + if (System.IO.Directory.Exists(fulldirectory) == false) + { + return; + } + + var files = System.IO.Directory.GetFiles(fulldirectory); + var listFile = new List<string>(); + foreach (var file in files) + { + var f = file.Substring(fulldirectory.Length + 1); + listFile.Add(f); + } + + var rootPath = Common.Config.Instance.FullPath; + foreach (var file in listFile) + { + if (file.StartsWith("ModelData_") == true) + { + //澶嶅埗妯℃澘鏁版嵁鏂囦欢鍒版寚瀹氭枃浠跺す + TemplateData.TemplateCommonLogic.Current.CopyTemplateFileToLocalDirectory2(System.IO.Path.Combine(fulldirectory, file)); + continue; + } + string oldFile = System.IO.Path.Combine(fulldirectory, file); + string newFile = System.IO.Path.Combine(rootPath, file); + //绉诲姩鏂囦欢 + this.MoveFileToDirectory(oldFile, newFile); + } + if (deleteDirectory == true) + { + //鍒犻櫎鏂囦欢澶� + this.DeleteDirectory(fulldirectory); + } + } + + /// <summary> + /// 灏嗘寚瀹氭枃浠跺す閲岄潰鐨勫叏閮ㄦ枃浠跺鍒跺埌鎸囧畾鐨勬枃浠跺す鍐� + /// </summary> + /// <param name="fullDirectory">澶嶅埗鍘熸枃浠跺す鍏ㄨ矾寰�</param> + /// <param name="targetDirectory">鐩爣鏂囦欢澶瑰叏璺緞</param> + public void CopyDirectoryFileToDirectory(string fullDirectory, string targetDirectory) + { + if (System.IO.Directory.Exists(targetDirectory) == false) + { + //鍒涘缓鐩爣鏂囦欢澶� + this.CreateDirectory(targetDirectory, false); + } + + var listFile = this.GetFileFromDirectory(fullDirectory); + foreach (var file in listFile) + { + string oldFile = System.IO.Path.Combine(fullDirectory, file); + string newFile = System.IO.Path.Combine(targetDirectory, file); + //澶嶅埗鏂囦欢 + this.CopyFile(oldFile, newFile); + } + } + + #endregion + + #region 鈻� 鏂囦欢鎿嶄綔鍑芥暟_______________________ + + /// <summary> + /// 鍒犻櫎鏂囦欢 + /// </summary> + /// <param name="fullName">鏂囦欢鍏ㄥ悕</param> + /// <returns></returns> + public bool DeleteFile(string fullName) + { + if (System.IO.File.Exists(fullName) == true) + { + try + { + System.IO.File.Delete(fullName); + return true; + } + catch { return false; } + } + return true; + } + + /// <summary> + /// 绉诲姩鏂囦欢 + /// </summary> + /// <param name="oldFile">鍘熸枃浠�</param> + /// <param name="newFile">鐩爣鏂囦欢</param> + public void MoveFileToDirectory(string oldFile, string newFile) + { + if (System.IO.File.Exists(oldFile) == true) + { + //濡傛灉鐩爣鏂囦欢瀛樺湪,鍒欏垏鎹负澶嶅埗鏂囦欢 + if (System.IO.File.Exists(newFile) == true) + { + this.CopyFile(oldFile, newFile); + } + try + { + System.IO.File.Move(oldFile, newFile); + } + catch (Exception ex) + { + HdlLogLogic.Current.WriteLog(ex, "绉诲姩澶辫触,鍘熸枃浠�:" + oldFile + "\r\n鐩爣鏂囦欢:" + newFile); + } + } + } + + /// <summary> + /// 绉诲姩鏂囦欢 + /// </summary> + /// <param name="oldFile">鍘熸枃浠�</param> + /// <param name="newFile">鐩爣鏂囦欢</param> + public void CopyFile(string oldFile, string newFile) + { + if (System.IO.File.Exists(oldFile) == true) + { + try + { + System.IO.File.Copy(oldFile, newFile, true); + } + catch (Exception ex) + { + HdlLogLogic.Current.WriteLog(ex, "澶嶅埗澶辫触,鍘熸枃浠�:" + oldFile + "\r\n鐩爣鏂囦欢:" + newFile); + } + } + } + + + #endregion + + #region 鈻� 鍒犻櫎鏈湴鏂囦欢_______________________ + + /// <summary> + /// 鍒犻櫎鏈湴鎵�鏈夋枃浠� + /// </summary> + /// <param name="all">true:鍏ㄩ儴鍒犻櫎(鐢ㄤ簬浣忓畢鍒犻櫎) false:閲嶈鐨勬枃浠朵笉鍒犻櫎</param> + public void DeleteAllLocationFile(bool all = true) + { + string dPath = Common.Config.Instance.FullPath; + if (System.IO.Directory.Exists(dPath) == false) + { + return; + } + + //鐒跺悗鑾峰彇鍏ㄩ儴鐨勬枃浠� + List<string> listFile = this.GetFileFromDirectory(dPath); + foreach (string file in listFile) + { + if (all == false && IsNotDeleteFile(file) == true) + { + //杩欐槸涓嶈兘鍒犻櫎鐨勬枃浠� + continue; + } + //鍒犻櫎鏂囦欢 + this.DeleteFile(System.IO.Path.Combine(dPath, file)); + } + //濡傛灉鏄妸鏂囦欢鍏ㄩ儴鍒犻櫎鐨勮瘽锛岄偅涔堟枃浠跺す涔熶竴璧峰垹闄ゆ帀 + if (all == true) + { + //鍒犻櫎鏂囦欢澶� + System.IO.Directory.Delete(dPath, true); + } + } + + /// <summary> + /// 鍒ゆ柇鏄笉鏄笉搴旇鍒犻櫎鐨勬枃浠� + /// </summary> + /// <param name="fileName"></param> + /// <returns></returns> + private bool IsNotDeleteFile(string fileName) + { + if (fileName == "Config.json") + { + //涓嶈兘鍒犻櫎Config鏂囦欢 + return true; + } + else if (fileName.StartsWith("House_") == true) + { + //涓嶈兘鍒犻櫎浣忓畢鏂囦欢 + return true; + } + return false; + } + + #endregion + } +} -- Gitblit v1.8.0