革博士程序V1仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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