革博士程序V1仓库
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

119 строки
4.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. namespace LeatherApp.Utils
  9. {
  10. internal class FileUtil
  11. {
  12. /// <summary>
  13. /// 另存为
  14. /// </summary>
  15. /// <param name="defaultFileName">默认文件名</param>
  16. /// <param name="filter">如 图像文件|*.jpg|图像文件|*.png|所有文件|*.*</param>
  17. /// <returns></returns>
  18. public static string saveAsFile(string defaultFileName, string filter= "所有文件|*.*")
  19. {
  20. SaveFileDialog s = new SaveFileDialog();
  21. s.Title = "保存文件";
  22. s.Filter = filter; //"图像文件|*.jpg|图像文件|*.png|所有文件|*.*";
  23. //s.DefaultExt = "图像文件|*.jpg"; //默认扩展名
  24. //s.InitialDirectory = @"C:\Users\Administrator\Desktop"; //保存的初始目录
  25. s.FileName = defaultFileName;//默认文件名
  26. if (s.ShowDialog() == DialogResult.OK)
  27. return s.FileName;
  28. return "";
  29. }
  30. /// <summary>
  31. /// 选择文件
  32. /// </summary>
  33. /// <param name="filter">如 图像文件|*.jpg|图像文件|*.png|所有文件|*.*</param>
  34. /// <returns></returns>
  35. public static string selectFile(string filter = "所有文件|*.*", string defaultPath = "")
  36. {
  37. OpenFileDialog fie = new OpenFileDialog();
  38. fie.Title = "选择文件";
  39. //fie.InitialDirectory = (defaultPath != "" ? defaultPath : Path.GetFullPath("."));
  40. fie.Filter = filter; //设置文件类型
  41. if (fie.ShowDialog() == DialogResult.OK)
  42. return fie.FileName;
  43. return "";
  44. }
  45. /// <summary>
  46. /// 选择目录(最后都带一个 '\')
  47. /// </summary>
  48. /// <returns></returns>
  49. public static string selectFolder(string defaultPath="")
  50. {
  51. FolderBrowserDialog dlg = new FolderBrowserDialog();
  52. dlg.SelectedPath = (string.IsNullOrWhiteSpace(defaultPath) ? Environment.GetFolderPath(Environment.SpecialFolder.Desktop) : defaultPath);
  53. if (dlg.ShowDialog() == DialogResult.OK)
  54. {
  55. return dlg.SelectedPath.TrimEnd(new char[] { '\\'})+"\\";
  56. }
  57. return "";
  58. }
  59. /// <summary>
  60. /// 删除指定目录下文件
  61. /// </summary>
  62. /// <param name="defaultPath"></param>
  63. /// <param name="fileExtend"></param>
  64. /// <returns></returns>
  65. public static bool delFilesInFolder(string FolderPath,string fileExtend="*")
  66. {
  67. if(!Directory.Exists(FolderPath))
  68. return false;
  69. string[] files=Directory.GetFiles(FolderPath, fileExtend, SearchOption.TopDirectoryOnly);
  70. foreach (string file in files)
  71. API.DeleteFile(file);
  72. return true;
  73. }
  74. /// <summary>
  75. /// 写INI文件
  76. /// </summary>
  77. /// <param name="Section">Section</param>
  78. /// <param name="Key">Key</param>
  79. /// <param name="value">value</param>
  80. public static void WriteIniValue(string filePath, string Section, string Key, string value)
  81. {
  82. WINAPI.WritePrivateProfileString(Section, Key, value, filePath);
  83. }
  84. /// <summary>
  85. /// 读取INI文件指定部分
  86. /// </summary>
  87. /// <param name="Section">Section</param>
  88. /// <param name="Key">Key</param>
  89. /// <returns>String</returns>
  90. public static string ReadIniValue(string filePath, string Section, string Key)
  91. {
  92. StringBuilder temp = new StringBuilder(255);
  93. int i = WINAPI.GetPrivateProfileString(Section, Key, "", temp, 255, filePath);
  94. return temp.ToString().Trim();
  95. }
  96. /// <summary>
  97. /// 自动检测并创建目录,并返回目录
  98. /// </summary>
  99. /// <param name="path"></param>
  100. /// <returns></returns>
  101. public static string initFolder(string dirPath)
  102. {
  103. if (!Directory.Exists(dirPath))
  104. Directory.CreateDirectory(dirPath);
  105. return dirPath;
  106. }
  107. }
  108. }