OpenFileDialogEx.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. namespace TSZ.DotNetDll.WinForms
  2. {
  3. using System;
  4. using System.IO;
  5. using System.Windows.Forms;
  6. using TSZ.DotNetDll;
  7. public class OpenFileDialogEx : IDisposable
  8. {
  9. private bool __Disposed;
  10. private OpenFileDialog dialog;
  11. private string flagGuid;
  12. public OpenFileDialogEx() : this(null)
  13. {
  14. }
  15. public OpenFileDialogEx(string flagGuid)
  16. {
  17. this.flagGuid = flagGuid;
  18. this.dialog = new OpenFileDialog();
  19. try
  20. {
  21. string keyValue = RegistryHelper.GetKeyValue(this.REGISTRYKEYFLAG);
  22. if (!string.IsNullOrEmpty(keyValue) && Directory.Exists(keyValue))
  23. {
  24. this.dialog.InitialDirectory = keyValue;
  25. }
  26. }
  27. catch
  28. {
  29. }
  30. }
  31. public void Dispose()
  32. {
  33. if (!this.__Disposed)
  34. {
  35. this.dialog.Dispose();
  36. this.__Disposed = true;
  37. }
  38. }
  39. public void Reset()
  40. {
  41. this.dialog.Reset();
  42. }
  43. public DialogResult ShowDialog()
  44. {
  45. return this.dialog.ShowDialog();
  46. }
  47. public DialogResult ShowDialog(IWin32Window owner)
  48. {
  49. return this.dialog.ShowDialog(owner);
  50. }
  51. public bool CheckFileExists
  52. {
  53. get
  54. {
  55. return this.dialog.CheckFileExists;
  56. }
  57. set
  58. {
  59. this.dialog.CheckFileExists = value;
  60. }
  61. }
  62. public string DefaultExt
  63. {
  64. get
  65. {
  66. return this.dialog.DefaultExt;
  67. }
  68. set
  69. {
  70. this.dialog.DefaultExt = value;
  71. }
  72. }
  73. public string FileName
  74. {
  75. get
  76. {
  77. string fileName = this.dialog.FileName;
  78. if (File.Exists(fileName))
  79. {
  80. string directoryName = Path.GetDirectoryName(fileName);
  81. if (string.IsNullOrEmpty(directoryName))
  82. {
  83. return fileName;
  84. }
  85. try
  86. {
  87. RegistryHelper.SetKeyValue(this.REGISTRYKEYFLAG, directoryName);
  88. }
  89. catch
  90. {
  91. }
  92. }
  93. return fileName;
  94. }
  95. set
  96. {
  97. this.dialog.FileName = value;
  98. }
  99. }
  100. public string[] FileNames
  101. {
  102. get
  103. {
  104. if ((this.dialog.FileNames != null) && (this.dialog.FileNames.Length > 0))
  105. {
  106. string path = this.dialog.FileNames[0];
  107. if (File.Exists(path))
  108. {
  109. string directoryName = Path.GetDirectoryName(path);
  110. if (!string.IsNullOrEmpty(directoryName))
  111. {
  112. try
  113. {
  114. RegistryHelper.SetKeyValue(this.REGISTRYKEYFLAG, directoryName);
  115. }
  116. catch
  117. {
  118. }
  119. }
  120. }
  121. }
  122. return this.dialog.FileNames;
  123. }
  124. }
  125. public string Filter
  126. {
  127. get
  128. {
  129. return this.dialog.Filter;
  130. }
  131. set
  132. {
  133. this.dialog.Filter = value;
  134. }
  135. }
  136. public bool Multiselect
  137. {
  138. get
  139. {
  140. return this.dialog.Multiselect;
  141. }
  142. set
  143. {
  144. this.dialog.Multiselect = value;
  145. }
  146. }
  147. private string REGISTRYKEYFLAG
  148. {
  149. get
  150. {
  151. if (string.IsNullOrEmpty(this.flagGuid))
  152. {
  153. return "FileBrowserDialogSelectedPath";
  154. }
  155. return (@"OpenFilePaths\" + this.flagGuid);
  156. }
  157. }
  158. public object Tag
  159. {
  160. get
  161. {
  162. return this.dialog.Tag;
  163. }
  164. set
  165. {
  166. this.dialog.Tag = value;
  167. }
  168. }
  169. public string Title
  170. {
  171. get
  172. {
  173. return this.dialog.Title;
  174. }
  175. set
  176. {
  177. this.dialog.Title = value;
  178. }
  179. }
  180. }
  181. }