/** *┌──────────────────────────────────────────────────────────────┐ *│ 描 述:文件IO的工具类 *│ 作 者:执笔小白 *│ 版 本:4.0 *│ 创建时间:2022-6-13 15:40:56 *└──────────────────────────────────────────────────────────────┘ *┌──────────────────────────────────────────────────────────────┐ *│ 命名空间: ZhibiXiaobai.Uril.IOHelper *│ 类 名:FileIOHelper *└──────────────────────────────────────────────────────────────┘ */ using System.IO; using System.Windows.Forms; namespace MainForm.ClassFile { public class FileIOHelper { /// /// 判断文件存不存在 /// 文件路径 /// 不存在时是否创建文件 /// 返回判断的结果 public static bool ISExists_File(string FilePath, bool IsCreate = false) { if (File.Exists(FilePath)) { return true; } else { if (IsCreate) // 需不需要创建文件 { //检验目录不存在就创建 //if (!System.IO.Directory.Exists(FilePath)) //{ // System.IO.Directory.CreateDirectory(FilePath); //} FilePath = FilePath.Replace(@"\\", @"/"); // 格式化目录 '\\'变为'/' string[] ss = FilePath.Split(new char[] { '/' }); string filePathP = ss[0]; for (int i = 1; i < ss.Length - 1; i++) { filePathP += "/" + ss[i]; } if (!System.IO.Directory.Exists(filePathP)) { System.IO.Directory.CreateDirectory(filePathP); } File.Create(FilePath); } return false; } } #region 通过路径取文件方法 /// /// 通过文件对话框取文件路径 /// 使用 var filename = OpenfileDlg(); /// /// 默认打开路径 /// 文件类型 /// 是否支持选择多个文件 /// Dlg窗体的标题 /// 是否限制大小(单位为KB,0为不限制) /// public static string OpenfileDlg(string Defaultpath = null, string Fileter = ".jpeg|*.jpeg|.jpg|*.jpg|.png|*.png|.gif|*.gif|.bmp|*.bmp", bool Multiselect = false, string DlgTitle = "请选择要打开的文件", long LimitSize = 0) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = DlgTitle; // 标题 ofd.Multiselect = Multiselect; // 多选 ofd.InitialDirectory = Defaultpath; // 初始目录 ofd.Filter = Fileter; // 设定文件类型 ofd.ShowDialog().GetHashCode(); string path = ofd.FileName; // 获得在打开文件对话框中选择的文件的路径 if (!string.IsNullOrEmpty(path) && LimitSize > 0 && ofd.OpenFile().Length > (LimitSize * 1024)) { path = null; } return path; } #endregion /// /// 复制-不覆盖已有文件 /// /// 源文件地址 /// 目标地址 public static void CopyFile(string SourceFileUrl, string DistFileUrl) { File.Copy(SourceFileUrl, DistFileUrl); } } }