SaveFileDialogEx.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 SaveFileDialogEx : IDisposable
  8. {
  9. private bool __Disposed;
  10. private SaveFileDialog dialog;
  11. private string flagGuid;
  12. public SaveFileDialogEx() : this(null)
  13. {
  14. }
  15. public SaveFileDialogEx(string flagGuid)
  16. {
  17. this.flagGuid = flagGuid;
  18. this.dialog = new SaveFileDialog();
  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. string directoryName = Path.GetDirectoryName(fileName);
  79. if (!string.IsNullOrEmpty(directoryName))
  80. {
  81. try
  82. {
  83. RegistryHelper.SetKeyValue(this.REGISTRYKEYFLAG, directoryName);
  84. }
  85. catch
  86. {
  87. }
  88. }
  89. return fileName;
  90. }
  91. set
  92. {
  93. this.dialog.FileName = value;
  94. }
  95. }
  96. public string Filter
  97. {
  98. get
  99. {
  100. return this.dialog.Filter;
  101. }
  102. set
  103. {
  104. this.dialog.Filter = value;
  105. }
  106. }
  107. private string REGISTRYKEYFLAG
  108. {
  109. get
  110. {
  111. if (string.IsNullOrEmpty(this.flagGuid))
  112. {
  113. return "FileSaveDialogSelectedPath";
  114. }
  115. return (@"SaveFilePaths\" + this.flagGuid);
  116. }
  117. }
  118. public object Tag
  119. {
  120. get
  121. {
  122. return this.dialog.Tag;
  123. }
  124. set
  125. {
  126. this.dialog.Tag = value;
  127. }
  128. }
  129. public string Title
  130. {
  131. get
  132. {
  133. return this.dialog.Title;
  134. }
  135. set
  136. {
  137. this.dialog.Title = value;
  138. }
  139. }
  140. }
  141. }