FileIOHelper.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. *┌──────────────────────────────────────────────────────────────┐
  3. *│ 描 述:文件IO的工具类
  4. *│ 作 者:执笔小白
  5. *│ 版 本:4.0
  6. *│ 创建时间:2022-6-13 15:40:56
  7. *└──────────────────────────────────────────────────────────────┘
  8. *┌──────────────────────────────────────────────────────────────┐
  9. *│ 命名空间: ZhibiXiaobai.Uril.IOHelper
  10. *│ 类 名:FileIOHelper
  11. *└──────────────────────────────────────────────────────────────┘
  12. */
  13. using System.IO;
  14. using System.Windows.Forms;
  15. namespace MainForm.ClassFile
  16. {
  17. public class FileIOHelper
  18. {
  19. /// <summary>
  20. /// 判断文件存不存在
  21. /// <param name="FilePath">文件路径</param>
  22. /// <param name="IsCreate">不存在时是否创建文件</param>
  23. /// <returns>返回判断的结果</returns>
  24. public static bool ISExists_File(string FilePath, bool IsCreate = false)
  25. {
  26. if (File.Exists(FilePath))
  27. {
  28. return true;
  29. }
  30. else
  31. {
  32. if (IsCreate) // 需不需要创建文件
  33. {
  34. //检验目录不存在就创建
  35. //if (!System.IO.Directory.Exists(FilePath))
  36. //{
  37. // System.IO.Directory.CreateDirectory(FilePath);
  38. //}
  39. FilePath = FilePath.Replace(@"\\", @"/"); // 格式化目录 '\\'变为'/'
  40. string[] ss = FilePath.Split(new char[] { '/' });
  41. string filePathP = ss[0];
  42. for (int i = 1; i < ss.Length - 1; i++)
  43. {
  44. filePathP += "/" + ss[i];
  45. }
  46. if (!System.IO.Directory.Exists(filePathP))
  47. {
  48. System.IO.Directory.CreateDirectory(filePathP);
  49. }
  50. File.Create(FilePath);
  51. }
  52. return false;
  53. }
  54. }
  55. #region 通过路径取文件方法
  56. /// <summary>
  57. /// 通过文件对话框取文件路径
  58. /// 使用 var filename = OpenfileDlg();
  59. /// </summary>
  60. /// <param name="Defaultpath">默认打开路径</param>
  61. /// <param name="Fileter">文件类型</param>
  62. /// <param name="Multiselect">是否支持选择多个文件</param>
  63. /// <param name="DlgTitle">Dlg窗体的标题</param>
  64. /// <param name="LimitSize">是否限制大小(单位为KB,0为不限制)</param>
  65. /// <returns></returns>
  66. public static string OpenfileDlg(string Defaultpath = null, string Fileter = ".jpeg|*.jpeg|.jpg|*.jpg|.png|*.png|.gif|*.gif|.bmp|*.bmp", bool Multiselect = false,
  67. string DlgTitle = "请选择要打开的文件", long LimitSize = 0)
  68. {
  69. OpenFileDialog ofd = new OpenFileDialog();
  70. ofd.Title = DlgTitle; // 标题
  71. ofd.Multiselect = Multiselect; // 多选
  72. ofd.InitialDirectory = Defaultpath; // 初始目录
  73. ofd.Filter = Fileter; // 设定文件类型
  74. ofd.ShowDialog().GetHashCode();
  75. string path = ofd.FileName; // 获得在打开文件对话框中选择的文件的路径
  76. if (!string.IsNullOrEmpty(path) && LimitSize > 0 && ofd.OpenFile().Length > (LimitSize * 1024))
  77. {
  78. path = null;
  79. }
  80. return path;
  81. }
  82. #endregion
  83. /// <summary>
  84. /// 复制-不覆盖已有文件
  85. /// </summary>
  86. /// <param name="SourceFileUrl">源文件地址</param>
  87. /// <param name="DistFileUrl">目标地址</param>
  88. public static void CopyFile(string SourceFileUrl, string DistFileUrl)
  89. {
  90. File.Copy(SourceFileUrl, DistFileUrl);
  91. }
  92. }
  93. }