old mode 100755
new mode 100644
| | |
| | | using 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>
|
| | | /// 读取文件,不限制住宅(文件不存在返回null,整天忘记,所以建一个函数来玩玩)
|
| | | /// </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>
|
| | | /// 读取文件,不限制住宅,文件不存在返回null
|
| | | /// </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">单纯只是获取文件名字,如果为false时,将返回文件的全路径</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
|
| | | }
|
| | | }
|
| | | using 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> |
| | | /// 读取文件,不限制住宅(文件不存在返回null,整天忘记,所以建一个函数来玩玩) |
| | | /// </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> |
| | | /// 读取文件,不限制住宅,文件不存在返回null |
| | | /// </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">单纯只是获取文件名字,如果为false时,将返回文件的全路径</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 |
| | | } |
| | | } |