ShortcutHelper.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. namespace GDNXFD.Alert.Config.Helpers
  2. {
  3. using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
  4. using MS.WindowsAPICodePack.Internal;
  5. using System;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. /// <summary>
  11. /// Shortcut Helper
  12. /// </summary>
  13. public static class ShortcutHelper
  14. {
  15. private static string AppId;
  16. /// <summary>
  17. /// This method creates a short cut.
  18. /// </summary>
  19. /// <param name="appid"></param>
  20. /// <returns></returns>
  21. public static bool TryCreateShortcut(string appid)
  22. {
  23. //var patata = System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
  24. //Path.Combine(patata, "programs", string.Format("{0}.lnk", appid));
  25. AppId = appid;
  26. String shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + string.Format("\\Microsoft\\Windows\\Start Menu\\Programs\\{0}.lnk", appid);
  27. if (!File.Exists(shortcutPath))
  28. {
  29. InstallShortcut(shortcutPath);
  30. return true;
  31. }
  32. return false;
  33. }
  34. private static void InstallShortcut(String shortcutPath)
  35. {
  36. // Find the path to the current executable
  37. String exePath = Process.GetCurrentProcess().MainModule.FileName;
  38. var newShortcut = (IShellLinkW)new CShellLink();
  39. // Create a shortcut to the exe
  40. ErrorHelper.VerifySucceeded(newShortcut.SetPath(exePath));
  41. ErrorHelper.VerifySucceeded(newShortcut.SetArguments(""));
  42. // Open the shortcut property store, set the AppUserModelId property
  43. var newShortcutProperties = (IPropertyStore)newShortcut;
  44. using (var appId = new PropVariant(AppId))
  45. {
  46. ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, appId));
  47. ErrorHelper.VerifySucceeded(newShortcutProperties.Commit());
  48. }
  49. // Commit the shortcut to disk
  50. var newShortcutSave = (IPersistFile)newShortcut;
  51. ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true));
  52. }
  53. internal enum STGM : long
  54. {
  55. STGM_READ = 0x00000000L,
  56. STGM_WRITE = 0x00000001L,
  57. STGM_READWRITE = 0x00000002L,
  58. STGM_SHARE_DENY_NONE = 0x00000040L,
  59. STGM_SHARE_DENY_READ = 0x00000030L,
  60. STGM_SHARE_DENY_WRITE = 0x00000020L,
  61. STGM_SHARE_EXCLUSIVE = 0x00000010L,
  62. STGM_PRIORITY = 0x00040000L,
  63. STGM_CREATE = 0x00001000L,
  64. STGM_CONVERT = 0x00020000L,
  65. STGM_FAILIFTHERE = 0x00000000L,
  66. STGM_DIRECT = 0x00000000L,
  67. STGM_TRANSACTED = 0x00010000L,
  68. STGM_NOSCRATCH = 0x00100000L,
  69. STGM_NOSNAPSHOT = 0x00200000L,
  70. STGM_SIMPLE = 0x08000000L,
  71. STGM_DIRECT_SWMR = 0x00400000L,
  72. STGM_DELETEONRELEASE = 0x04000000L,
  73. }
  74. internal static class ShellIIDGuid
  75. {
  76. internal const string IShellLinkW = "000214F9-0000-0000-C000-000000000046";
  77. internal const string CShellLink = "00021401-0000-0000-C000-000000000046";
  78. internal const string IPersistFile = "0000010b-0000-0000-C000-000000000046";
  79. internal const string IPropertyStore = "886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99";
  80. }
  81. [ComImport, Guid(ShellIIDGuid.IShellLinkW), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  82. internal interface IShellLinkW
  83. {
  84. UInt32 GetPath([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, IntPtr pfd, uint fFlags);
  85. UInt32 GetIDList(out IntPtr ppidl);
  86. UInt32 SetIDList(IntPtr pidl);
  87. UInt32 GetDescription([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxName);
  88. UInt32 SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
  89. UInt32 GetWorkingDirectory([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
  90. UInt32 SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
  91. UInt32 GetArguments([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
  92. UInt32 SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
  93. UInt32 GetHotKey(out short wHotKey);
  94. UInt32 SetHotKey(short wHotKey);
  95. UInt32 GetShowCmd(out uint iShowCmd);
  96. UInt32 SetShowCmd(uint iShowCmd);
  97. UInt32 GetIconLocation([Out(), MarshalAs(UnmanagedType.LPWStr)] out StringBuilder pszIconPath, int cchIconPath, out int iIcon);
  98. UInt32 SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
  99. UInt32 SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, uint dwReserved);
  100. UInt32 Resolve(IntPtr hwnd, uint fFlags);
  101. UInt32 SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
  102. }
  103. [ComImport, Guid(ShellIIDGuid.IPersistFile), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  104. internal interface IPersistFile
  105. {
  106. UInt32 GetCurFile([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile);
  107. UInt32 IsDirty();
  108. UInt32 Load([MarshalAs(UnmanagedType.LPWStr)] string pszFileName, [MarshalAs(UnmanagedType.U4)] STGM dwMode);
  109. UInt32 Save([MarshalAs(UnmanagedType.LPWStr)] string pszFileName, bool fRemember);
  110. UInt32 SaveCompleted([MarshalAs(UnmanagedType.LPWStr)] string pszFileName);
  111. }
  112. [ComImport]
  113. [Guid(ShellIIDGuid.IPropertyStore)]
  114. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  115. interface IPropertyStore
  116. {
  117. UInt32 GetCount([Out] out uint propertyCount);
  118. UInt32 GetAt([In] uint propertyIndex, out PropertyKey key);
  119. UInt32 GetValue([In] ref PropertyKey key, [Out] PropVariant pv);
  120. UInt32 SetValue([In] ref PropertyKey key, [In] PropVariant pv);
  121. UInt32 Commit();
  122. }
  123. [ComImport, Guid(ShellIIDGuid.CShellLink), ClassInterface(ClassInterfaceType.None)]
  124. internal class CShellLink { }
  125. /// <summary>
  126. /// Error Helper
  127. /// </summary>
  128. public static class ErrorHelper
  129. {
  130. /// <summary>
  131. /// Verify if an operation succeeded
  132. /// </summary>
  133. /// <param name="hresult"></param>
  134. public static void VerifySucceeded(UInt32 hresult)
  135. {
  136. if (hresult > 1)
  137. {
  138. throw new Exception("Failed with HRESULT: " + hresult.ToString("X"));
  139. }
  140. }
  141. }
  142. }
  143. }