CreateShortcut.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.Win32;
  5. using System.Runtime.InteropServices;
  6. namespace LYFZ.WinAPI
  7. {
  8. public class ShortcutAPI
  9. {
  10. [StructLayout(LayoutKind.Sequential)]
  11. public struct FILETIME
  12. {
  13. uint dwLowDateTime;
  14. uint dwHighDateTime;
  15. }
  16. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  17. public struct WIN32_FIND_DATA
  18. {
  19. public const int MAX_PATH = 260;
  20. uint dwFileAttributes;
  21. FILETIME ftCreationTime;
  22. FILETIME ftLastAccessTime;
  23. FILETIME ftLastWriteTime;
  24. uint nFileSizeHight;
  25. uint nFileSizeLow;
  26. uint dwOID;
  27. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
  28. string cFileName;
  29. }
  30. [ComImport]
  31. [Guid("0000010c-0000-0000-c000-000000000046")]
  32. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  33. public interface IPersist
  34. {
  35. [PreserveSig]
  36. void GetClassID(out Guid pClassID);
  37. }
  38. [ComImport]
  39. [Guid("0000010b-0000-0000-C000-000000000046")]
  40. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  41. public interface IPersistFile
  42. : IPersist
  43. {
  44. new void GetClassID(out Guid pClassID);
  45. [PreserveSig]
  46. int IsDirty();
  47. [PreserveSig]
  48. void Load(
  49. [MarshalAs(UnmanagedType.LPWStr)] string pszFileName,
  50. uint dwMode);
  51. [PreserveSig]
  52. void Save(
  53. [MarshalAs(UnmanagedType.LPWStr)] string pszFileName,
  54. [MarshalAs(UnmanagedType.Bool)] bool fRemember);
  55. [PreserveSig]
  56. void SaveCompleted([MarshalAs(UnmanagedType.LPWStr)] string pszFileName);
  57. [PreserveSig]
  58. void GetCurFile([MarshalAs(UnmanagedType.LPWStr)] string ppszFileName);
  59. }
  60. [ComImport]
  61. [Guid("000214F9-0000-0000-C000-000000000046")]
  62. [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  63. public interface IShellLink
  64. {
  65. [PreserveSig]
  66. void GetPath(
  67. [MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 1)] out string pszFile,
  68. int cch,
  69. ref WIN32_FIND_DATA pfd,
  70. uint fFlags);
  71. [PreserveSig]
  72. void GetIDList(out IntPtr ppidl);
  73. [PreserveSig]
  74. void SetIDList(IntPtr ppidl);
  75. [PreserveSig]
  76. void GetDescription(
  77. [MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 1)] out string pszName,
  78. int cch);
  79. [PreserveSig]
  80. void SetDescription(
  81. [MarshalAs(UnmanagedType.LPWStr)] string pszName);
  82. [PreserveSig]
  83. void GetWorkingDirectory(
  84. [MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 1)] out string pszDir,
  85. int cch);
  86. [PreserveSig]
  87. void SetWorkingDirectory(
  88. [MarshalAs(UnmanagedType.LPWStr)] string pszDir);
  89. [PreserveSig]
  90. void GetArguments(
  91. [MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 1)] out string pszArgs,
  92. int cch);
  93. [PreserveSig]
  94. void SetArguments(
  95. [MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
  96. [PreserveSig]
  97. void GetHotkey(out ushort pwHotkey);
  98. [PreserveSig]
  99. void SetHotkey(ushort wHotkey);
  100. [PreserveSig]
  101. void GetShowCmd(out int piShowCmd);
  102. [PreserveSig]
  103. void SetShowCmd(int iShowCmd);
  104. [PreserveSig]
  105. void GetIconLocation(
  106. [MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 1)] out string pszIconPath,
  107. int cch,
  108. out int piIcon);
  109. [PreserveSig]
  110. void SetIconLocation(
  111. [MarshalAs(UnmanagedType.LPWStr)] string pszIconPath,
  112. int iIcon);
  113. [PreserveSig]
  114. void SetRelativePath(
  115. [MarshalAs(UnmanagedType.LPWStr)] string pszPathRel,
  116. uint dwReserved);
  117. [PreserveSig]
  118. void Resolve(
  119. IntPtr hwnd,
  120. uint fFlags);
  121. [PreserveSig]
  122. void SetPath(
  123. [MarshalAs(UnmanagedType.LPWStr)] string pszFile);
  124. }
  125. [GuidAttribute("00021401-0000-0000-C000-000000000046")]
  126. [ClassInterfaceAttribute(ClassInterfaceType.None)]
  127. [ComImportAttribute()]
  128. public class CShellLink
  129. {
  130. }
  131. public const int SW_SHOWNORMAL = 1;
  132. /// <summary>
  133. /// 创建快捷方式。
  134. /// </summary>
  135. /// <param name="shortcutPath">快捷方式路径。</param>
  136. /// <param name="targetPath">目标路径。</param>
  137. /// <param name="workingDirectory">工作路径。</param>
  138. /// <param name="description">快捷键描述。</param>
  139. public static bool CreateShortcut(string shortcutPath, string targetPath, string workingDirectory, string description, string iconLocation = null)
  140. {
  141. try
  142. {
  143. CShellLink cShellLink = new CShellLink();
  144. IShellLink iShellLink = (IShellLink)cShellLink;
  145. iShellLink.SetDescription(description);
  146. iShellLink.SetShowCmd(SW_SHOWNORMAL);
  147. iShellLink.SetPath(targetPath);
  148. iShellLink.SetWorkingDirectory(workingDirectory);
  149. if (!string.IsNullOrEmpty(iconLocation))
  150. {
  151. iShellLink.SetIconLocation(iconLocation, 0);
  152. }
  153. IPersistFile iPersistFile = (IPersistFile)iShellLink;
  154. iPersistFile.Save(shortcutPath, false);
  155. Marshal.ReleaseComObject(iPersistFile);
  156. iPersistFile = null;
  157. Marshal.ReleaseComObject(iShellLink);
  158. iShellLink = null;
  159. Marshal.ReleaseComObject(cShellLink);
  160. cShellLink = null;
  161. return true;
  162. }
  163. catch //(System.Exception ex)
  164. {
  165. return false;
  166. }
  167. }
  168. /// <summary>
  169. /// 创建桌面快捷方式
  170. /// </summary>
  171. /// <param name="targetPath">可执行文件路径</param>
  172. /// <param name="description">快捷方式名称</param>
  173. /// <param name="iconLocation">快捷方式图标路径</param>
  174. /// <param name="workingDirectory">工作路径</param>
  175. /// <returns></returns>
  176. public static bool CreateDesktopShortcut(string targetPath, string description, string iconLocation = null, string workingDirectory = null)
  177. {
  178. if (string.IsNullOrEmpty(workingDirectory))
  179. {
  180. workingDirectory = ShortcutAPI.GetDeskDir();
  181. }
  182. return ShortcutAPI.CreateShortcut(ShortcutAPI.GetDeskDir() + "\\" + description + ".lnk", targetPath, workingDirectory, description, iconLocation);
  183. }
  184. /// <summary>
  185. /// 创建程序菜单快捷方式
  186. /// </summary>
  187. /// <param name="targetPath">可执行文件路径</param>
  188. /// <param name="description">快捷方式名称</param>
  189. /// <param name="menuName">程序菜单中子菜单名称,为空则不创建子菜单</param>
  190. /// <param name="iconLocation">快捷方式图标路径</param>
  191. /// <param name="workingDirectory">工作路径</param>
  192. /// <returns></returns>
  193. public static bool CreateProgramsShortcut(string targetPath, string description, string menuName, string iconLocation = null, string workingDirectory = null)
  194. {
  195. if (string.IsNullOrEmpty(workingDirectory))
  196. {
  197. workingDirectory = ShortcutAPI.GetProgramsDir();
  198. }
  199. string shortcutPath = ShortcutAPI.GetProgramsDir();
  200. if (!string.IsNullOrEmpty(menuName))
  201. {
  202. shortcutPath += "\\" + menuName;
  203. if (!System.IO.Directory.Exists(shortcutPath))
  204. {
  205. try
  206. {
  207. System.IO.Directory.CreateDirectory(shortcutPath);
  208. }
  209. catch //(System.Exception ex)
  210. {
  211. return false;
  212. }
  213. }
  214. }
  215. shortcutPath += "\\" + description + ".lnk";
  216. return ShortcutAPI.CreateShortcut(shortcutPath, targetPath, workingDirectory, description, iconLocation);
  217. }
  218. public static bool AddFavorites(string url, string description, string folderName, string iconLocation = null, string workingDirectory = null)
  219. {
  220. if (string.IsNullOrEmpty(workingDirectory))
  221. {
  222. workingDirectory = ShortcutAPI.GetProgramsDir();
  223. }
  224. string shortcutPath = ShortcutAPI.GetFavoriteDir();
  225. if (!string.IsNullOrEmpty(folderName))
  226. {
  227. shortcutPath += "\\" + folderName;
  228. if (!System.IO.Directory.Exists(shortcutPath))
  229. {
  230. try
  231. {
  232. System.IO.Directory.CreateDirectory(shortcutPath);
  233. }
  234. catch //(System.Exception ex)
  235. {
  236. return false;
  237. }
  238. }
  239. }
  240. shortcutPath += "\\" + description + ".lnk";
  241. return ShortcutAPI.CreateShortcut(shortcutPath, url, workingDirectory, description, iconLocation);
  242. }
  243. internal const uint SHGFP_TYPE_CURRENT = 0;
  244. internal const int MAX_PATH = 260;
  245. internal const uint CSIDL_COMMON_STARTMENU = 0x0016; // All Users\Start Menu
  246. internal const uint CSIDL_COMMON_PROGRAMS = 0x0017; // All Users\Start Menu\Programs
  247. internal const uint CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019; // All Users\Desktop
  248. internal const uint CSIDL_PROGRAM_FILES = 0x0026; // C:\Program Files
  249. internal const uint CSIDL_FLAG_CREATE = 0x8000; // new for Win2K, or this in to force creation of folder
  250. internal const uint CSIDL_COMMON_FAVORITES = 0x001f; // All Users Favorites
  251. [DllImport("shell32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
  252. internal static extern void SHGetFolderPathW(
  253. IntPtr hwndOwner,
  254. int nFolder,
  255. IntPtr hToken,
  256. uint dwFlags,
  257. IntPtr pszPath);
  258. internal static string SHGetFolderPath(int nFolder)
  259. {
  260. string pszPath = new string(' ', MAX_PATH);
  261. IntPtr bstr = Marshal.StringToBSTR(pszPath);
  262. SHGetFolderPathW(IntPtr.Zero, nFolder, IntPtr.Zero, SHGFP_TYPE_CURRENT, bstr);
  263. string path = Marshal.PtrToStringBSTR(bstr);
  264. int index = path.IndexOf('\0');
  265. string path2 = path.Substring(0, index);
  266. Marshal.FreeBSTR(bstr);
  267. return path2;
  268. }
  269. public static string GetSpecialFolderPath(uint csidl)
  270. {
  271. return SHGetFolderPath((int)(csidl | CSIDL_FLAG_CREATE));
  272. }
  273. public static string GetDeskDir()
  274. {
  275. return GetSpecialFolderPath(CSIDL_COMMON_DESKTOPDIRECTORY);
  276. }
  277. public static string GetProgramsDir()
  278. {
  279. return GetSpecialFolderPath(CSIDL_COMMON_PROGRAMS);
  280. }
  281. public static string GetFavoriteDir()
  282. {
  283. return GetSpecialFolderPath(CSIDL_COMMON_FAVORITES);
  284. }
  285. }
  286. }