Extensions.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using System.ComponentModel;
  8. using Microsoft.Win32;
  9. using SmartCoalApplication.SystemLayer.FileDlgExtenders.Win32Types;
  10. namespace SmartCoalApplication.SystemLayer.FileDlgExtenders.FileDialogExtenders
  11. {
  12. public static class Extensions
  13. {
  14. #region extension Methods
  15. public static DialogResult ShowDialog(this FileDialog fdlg, FileDialogControlBase ctrl, IWin32Window owner) //where T : FileDialogControlBase, new()
  16. {
  17. ctrl.FileDlgType = (fdlg is SaveFileDialog) ? FileDialogType.SaveFileDlg : FileDialogType.OpenFileDlg;
  18. if (ctrl.ShowDialogExt(fdlg, owner) == DialogResult.OK)
  19. return DialogResult.OK;
  20. else
  21. return DialogResult.Ignore;
  22. }
  23. #endregion
  24. }
  25. //see http://msdn.microsoft.com/en-us/magazine/cc300434.aspx
  26. public static class FileDialogPlaces
  27. {
  28. private static readonly string TempKeyName = "TempPredefKey_" + Guid.NewGuid().ToString();
  29. private const string Key_PlacesBar = @"Software\Microsoft\Windows\CurrentVersion\Policies\ComDlg32\PlacesBar";
  30. private static RegistryKey _fakeKey;
  31. private static IntPtr _overriddenKey;
  32. private static object[] m_places;
  33. public static void SetPlaces(this FileDialog fd, object[] places)
  34. {
  35. if (fd == null || places == null)
  36. return;
  37. if (m_places == null)
  38. m_places = new object[places.GetLength(0)];
  39. for (int i = 0; i < m_places.GetLength(0); i++)
  40. {
  41. m_places[i] = places[i];
  42. }
  43. if (_fakeKey != null)
  44. ResetPlaces(fd);
  45. SetupFakeRegistryTree();
  46. if (fd != null)
  47. fd.Disposed += (object sender, EventArgs e) => { if (m_places != null && fd != null) ResetPlaces(fd); };
  48. }
  49. static FileDialogPlaces()
  50. {
  51. }
  52. static public void ResetPlaces(this FileDialog fd)
  53. {
  54. if (_overriddenKey != IntPtr.Zero)
  55. {
  56. ResetRegistry(_overriddenKey);
  57. _overriddenKey = IntPtr.Zero;
  58. }
  59. if (_fakeKey != null)
  60. {
  61. _fakeKey.Close();
  62. _fakeKey = null;
  63. }
  64. //delete the key tree
  65. Registry.CurrentUser.DeleteSubKeyTree(TempKeyName);
  66. m_places = null;
  67. }
  68. private static void SetupFakeRegistryTree()
  69. {
  70. try
  71. {
  72. _fakeKey = Registry.CurrentUser.CreateSubKey(TempKeyName);
  73. _overriddenKey = InitializeRegistry();
  74. // at this point, m_TempKeyName equals places key
  75. // write dynamic places here reading from Places
  76. RegistryKey reg = Registry.CurrentUser.CreateSubKey(Key_PlacesBar);
  77. for (int i = 0; i < m_places.GetLength(0); i++)
  78. {
  79. if (m_places[i] != null)
  80. {
  81. reg.SetValue("Place" + i.ToString(), m_places[i]);
  82. }
  83. }
  84. }
  85. catch
  86. {
  87. }
  88. }
  89. //public static IntPtr GetRegistryHandle(RegistryKey registryKey)
  90. //{
  91. // Type type = registryKey.GetType();
  92. // FieldInfo fieldInfo = type.GetField("hkey", BindingFlags.Instance | BindingFlags.NonPublic);
  93. // return (IntPtr)fieldInfo.GetValue(registryKey);
  94. //}
  95. static readonly UIntPtr HKEY_CURRENT_USER = new UIntPtr(0x80000001u);
  96. private static IntPtr InitializeRegistry()
  97. {
  98. IntPtr hkMyCU;
  99. Win32Types.NativeMethods.RegCreateKeyW(HKEY_CURRENT_USER, TempKeyName, out hkMyCU);
  100. Win32Types.NativeMethods.RegOverridePredefKey(HKEY_CURRENT_USER, hkMyCU);
  101. return hkMyCU;
  102. }
  103. static void ResetRegistry(IntPtr hkMyCU)
  104. {
  105. try
  106. {
  107. Win32Types.NativeMethods.RegOverridePredefKey(HKEY_CURRENT_USER, IntPtr.Zero);
  108. Win32Types.NativeMethods.RegCloseKey(hkMyCU);
  109. }
  110. catch
  111. {
  112. }
  113. }
  114. }
  115. //http://www.codeguru.com/cpp/misc/misc/system/article.php/c13407/
  116. // fot .net 4.0 and up use CustomPlaces instead :http://msdn.microsoft.com/en-us/library/microsoft.win32.filedialog.customplaces.aspx
  117. public enum Places
  118. {
  119. [Description("Desktop")]
  120. Desktop = 0,
  121. [Description("Internet Explorer ")]
  122. InternetExplorer = 1,
  123. [Description("Program Files")]
  124. Programs = 2,
  125. [Description("Control Panel")]
  126. ControlPanel = 3,
  127. [Description("Printers")]
  128. Printers = 4,
  129. [Description("My Documents")]
  130. MyDocuments = 5,
  131. [Description("Favorites")]
  132. Favorites = 6,
  133. [Description("Startup folder")]
  134. StartupFolder = 7,
  135. [Description("Recent Files")]
  136. RecentFiles = 8,
  137. [Description("Send To")]
  138. SendTo = 9,
  139. [Description("Recycle Bin")]
  140. RecycleBin = 0xa,
  141. [Description("Start menu")]
  142. StartMenu = 0xb,
  143. [Description("Logical My Documents")]
  144. Logical_MyDocuments = 0xc,
  145. [Description("My Music")]
  146. MyMusic = 0xd,
  147. [Description("My Videos")]
  148. MyVideos = 0xe,
  149. [Description("<user name>\\Desktop")]
  150. UserName_Desktop = 0x10,
  151. [Description("My Computer")]
  152. MyComputer = 0x11,
  153. [Description("My Network Places")]
  154. MyNetworkPlaces = 18,
  155. [Description("<user name>\nethood")]
  156. User_Name_Nethood = 0x13,
  157. [Description("Fonts")]
  158. Fonts = 0x14,
  159. [Description("All Users\\Start Menu")]
  160. All_Users_StartMenu = 0x16,
  161. [Description("All Users\\Start Menu\\Programs ")]
  162. All_Users_StartMenu_Programs = 0x17,
  163. [Description("All Users\\Startup")]
  164. All_Users_Startup = 0x18,
  165. [Description("All Users\\Desktop")]
  166. All_Users_Desktop = 0x19,
  167. [Description("<user name>\\Application Data ")]
  168. User_name_ApplicationData = 0x1a,
  169. [Description("<user name>\\PrintHood ")]
  170. User_Name_PrintHood = 0x1b,
  171. [Description("<user name>\\Local Settings\\Applicaiton Data (nonroaming)")]
  172. Local_ApplicaitonData = 0x1c,
  173. [Description("Nonlocalized common startup ")]
  174. NonlocalizedCommonStartup = 0x1e,
  175. [Description("")]
  176. CommonFavorites = 0x1f,
  177. [Description("Internet Cache ")]
  178. InternetCache = 0x20,
  179. [Description("Cookies ")]
  180. Cookies = 0x21,
  181. [Description("History")]
  182. History = 0x22,
  183. [Description("All Users\\Application Data ")]
  184. All_Users_ApplicationData = 0x23,
  185. [Description("Windows Directory")]
  186. WindowsDirectory = 0x24,
  187. [Description("System Directory")]
  188. SystemDirectory = 0x25,
  189. [Description("Program Files ")]
  190. ProgramFiles = 0x26,
  191. [Description("My Pictures ")]
  192. MyPictures = 0x27,
  193. [Description("USERPROFILE")]
  194. USERPROFILE = 0x28,
  195. [Description("system directory on RISC")]
  196. SYSTEN_RISC = 0x29,
  197. [Description("Program Files on RISC ")]
  198. Program_Files_RISC = 0x2a,
  199. [Description("Program Files\\Common")]
  200. Common = 0x2b,
  201. [Description("Program Files\\Common on RISC")]
  202. Common_RISC = 0x2c,
  203. [Description("All Users\\Templates ")]
  204. Templates = 0x2d,
  205. [Description("All Users\\Documents")]
  206. All_Users_Documents = 0x2e,
  207. [Description("All Users\\Start Menu\\Programs\\Administrative Tools")]
  208. AdministrativeTools = 0x2f,
  209. [Description("<user name>\\Start Menu\\Programs\\Administrative Tools")]
  210. USER_AdministrativeTools = 0x30,
  211. [Description("Network and Dial-up Connections")]
  212. Network_DialUp_Connections = 0x31,
  213. [Description("All Users\\My Music")]
  214. All_Users_MyMusic = 0x35,
  215. [Description("All Users\\My Pictures")]
  216. All_Users_MyPictures = 0x36,
  217. [Description("All Users\\My Video")]
  218. All_Users_MyVideo = 0x37,
  219. [Description("Resource Directory")]
  220. Resource = 0x38,
  221. [Description("Localized Resource Directory ")]
  222. Localized_Resource = 0x39,
  223. [Description("OEM specific apps")]
  224. OEM_Specific = 0x3a,
  225. [Description("USERPROFILE\\Local Settings\\Application Data\\Microsoft\\CD Burning")]
  226. CDBurning = 0x3b
  227. }
  228. }