ChooseFileExtension.cs 3.9 KB

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