ChooseFileExtension.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using DevExpress.XtraEditors;
  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 ExtensionsDev
  10. {
  11. public static class ChooseFileExtension
  12. {
  13. /// <summary>
  14. /// 使用选择Wave文件的功能
  15. /// </summary>
  16. /// <param name="ctrl">ButtonEdit控件</param>
  17. /// <param name="onFileChanged">文件改变事件,参数为选择的文件和文件的采样率Hz</param>
  18. /// <returns></returns>
  19. public static ButtonEdit UseChooseWaveFile(this ButtonEdit ctrl, Action<string, int> onFileChanged = null)
  20. {
  21. ctrl.AllowDrop = true;
  22. ctrl.DragEnter += ctrl_DragEnter;
  23. ctrl.DragDrop += ctrl_DragDrop;
  24. ctrl.ButtonClick += Ctrl_ButtonClick;
  25. ctrl.TextChanged += (sender, e) =>
  26. {
  27. string file = (sender as Control).Text;
  28. if (!File.Exists(file) || onFileChanged == null) return;
  29. //从wav文件中自动读取采样率
  30. FileInfo info = new FileInfo(file);
  31. if (info.Extension.ToLower() == ".wav")
  32. {
  33. using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
  34. {
  35. fs.Position = 24;
  36. byte[] data = new byte[4];
  37. fs.Read(data, 0, 4);
  38. var fsHz = BitConverter.ToInt32(data, 0);
  39. if (fsHz > 0)
  40. {
  41. onFileChanged(file, fsHz);
  42. }
  43. };
  44. }
  45. else
  46. {
  47. onFileChanged(file, 0);
  48. }
  49. };
  50. ctrl.Tag = nameof(UseChooseFile);
  51. return ctrl;
  52. }
  53. /// <summary>
  54. /// 使用通用文件选择功能
  55. /// </summary>
  56. /// <param name="ctrl">ButtonEdit控件</param>
  57. /// <returns></returns>
  58. public static ButtonEdit UseChooseFile(this ButtonEdit ctrl)
  59. {
  60. ctrl.AllowDrop = true;
  61. ctrl.DragEnter += ctrl_DragEnter;
  62. ctrl.DragDrop += ctrl_DragDrop;
  63. ctrl.ButtonClick += Ctrl_ButtonClick;
  64. ctrl.Tag = nameof(UseChooseFile);
  65. return ctrl;
  66. }
  67. public static ButtonEdit UseChooseDir(this ButtonEdit ctrl)
  68. {
  69. ctrl.UseChooseFile();
  70. ctrl.Tag = nameof(UseChooseDir);
  71. return ctrl;
  72. }
  73. private static void Ctrl_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
  74. {
  75. var ctrl = sender as Control;
  76. if (ctrl.Tag?.ToString() == nameof(UseChooseFile))
  77. {
  78. XtraOpenFileDialog xtraOpenFileDialog1 = new XtraOpenFileDialog();
  79. xtraOpenFileDialog1.Multiselect = false;
  80. var currentDir = Environment.CurrentDirectory;
  81. if (xtraOpenFileDialog1.ShowDialog() == DialogResult.OK)
  82. {
  83. Environment.CurrentDirectory = currentDir;
  84. ctrl.Text = xtraOpenFileDialog1.FileName;
  85. }
  86. }
  87. else
  88. {
  89. XtraOpenFileDialog xtraOpenFileDialog1 = new XtraOpenFileDialog();
  90. xtraOpenFileDialog1.Multiselect = false;
  91. var currentDir = Environment.CurrentDirectory;
  92. if (xtraOpenFileDialog1.ShowDialog() == DialogResult.OK)
  93. {
  94. Environment.CurrentDirectory = currentDir;
  95. ctrl.Text = Path.GetDirectoryName(xtraOpenFileDialog1.FileName);
  96. }
  97. }
  98. }
  99. private static void ctrl_DragEnter(object sender, DragEventArgs e)
  100. {
  101. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  102. {
  103. e.Effect = DragDropEffects.Link;
  104. }
  105. else
  106. {
  107. e.Effect = DragDropEffects.None;
  108. }
  109. }
  110. private static void ctrl_DragDrop(object sender, DragEventArgs e)
  111. {
  112. var file = ((Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
  113. var ctrl = sender as ButtonEdit;
  114. bool dragFile = ctrl.Tag?.ToString() == nameof(UseChooseFile);
  115. if (dragFile)
  116. (sender as Control).Text = file;
  117. else
  118. {
  119. if (File.Exists(file))
  120. (sender as Control).Text = Path.GetDirectoryName(file);
  121. else
  122. (sender as Control).Text = file;
  123. }
  124. }
  125. }
  126. }