123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- /**
- *┌──────────────────────────────────────────────────────────────┐
- *│ 描 述:文件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
- {
- /// <summary>
- /// 判断文件存不存在
- /// <param name="FilePath">文件路径</param>
- /// <param name="IsCreate">不存在时是否创建文件</param>
- /// <returns>返回判断的结果</returns>
- 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 通过路径取文件方法
- /// <summary>
- /// 通过文件对话框取文件路径
- /// 使用 var filename = OpenfileDlg();
- /// </summary>
- /// <param name="Defaultpath">默认打开路径</param>
- /// <param name="Fileter">文件类型</param>
- /// <param name="Multiselect">是否支持选择多个文件</param>
- /// <param name="DlgTitle">Dlg窗体的标题</param>
- /// <param name="LimitSize">是否限制大小(单位为KB,0为不限制)</param>
- /// <returns></returns>
- 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
- /// <summary>
- /// 复制-不覆盖已有文件
- /// </summary>
- /// <param name="SourceFileUrl">源文件地址</param>
- /// <param name="DistFileUrl">目标地址</param>
- public static void CopyFile(string SourceFileUrl, string DistFileUrl)
- {
- File.Copy(SourceFileUrl, DistFileUrl);
- }
- }
- }
|