版博士V2.0程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

88 righe
3.2 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 AssistClient.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 openFile(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 = (defaultPath != "" ? defaultPath : Path.GetFullPath("."));
  53. if (dlg.ShowDialog() == DialogResult.OK)
  54. return dlg.SelectedPath;
  55. return "";
  56. }
  57. /// <summary>
  58. /// 写INI文件
  59. /// </summary>
  60. /// <param name="Section">Section</param>
  61. /// <param name="Key">Key</param>
  62. /// <param name="value">value</param>
  63. public static void WriteIniValue(string filePath, string Section, string Key, string value)
  64. {
  65. WINAPI.WritePrivateProfileString(Section, Key, value, filePath);
  66. }
  67. /// <summary>
  68. /// 读取INI文件指定部分
  69. /// </summary>
  70. /// <param name="Section">Section</param>
  71. /// <param name="Key">Key</param>
  72. /// <returns>String</returns>
  73. public static string ReadIniValue(string filePath, string Section, string Key)
  74. {
  75. StringBuilder temp = new StringBuilder(255);
  76. int i = WINAPI.GetPrivateProfileString(Section, Key, "", temp, 255, filePath);
  77. return temp.ToString().Trim();
  78. }
  79. }
  80. }