C # Create desktop shortcuts, create program menu items, add Web pages to favorites with pure code
Start Menu Program menu item:
Add to Favorites:
Related function code:
public const int SW_SHOWNORMAL = 1; <summary>//Create a shortcut. </summary>//<param name= "Shortcutpath" > Shortcut path. </param>//<param name= "TargetPath" > Target path. </param>//<param name= "workingdirectory" > Work path. </param>//<param name= "description" > Shortcut key description. </param> public static bool CreateShortcut (string Shortcutpath, String TargetPath, string workingdirectory, S Tring Description, String iconlocation = null) {try {Cshelllink Cshelllink = New Cshelllink (); IShellLink IShellLink = (ishelllink) Cshelllink; Ishelllink.setdescription (description); Ishelllink.setshowcmd (SW_SHOWNORMAL); Ishelllink.setpath (TargetPath); Ishelllink.setworkingdirectory (workingdirectory); if (!string. IsNullOrEmpty (iconlocation)) {ishelllInk. Seticonlocation (iconlocation, 0); } ipersistfile IPersistFile = (ipersistfile) IShellLink; Ipersistfile.save (Shortcutpath, false); Marshal.ReleaseComObject (IPersistFile); IPersistFile = null; Marshal.ReleaseComObject (IShellLink); IShellLink = null; Marshal.ReleaseComObject (Cshelllink); Cshelllink = null; return true; } catch//(System.Exception ex) {return false; } }
<summary>//Create desktop shortcuts///</summary>//<param name= "TargetPath" > Executable path </ param>//<param name= "description" > Shortcut name </param>//<param name= "IconLocation" > Shortcuts Icon path </param>///<param Name= "workingdirectory" > Work path </param>//<returns></retur ns> public static bool Createdesktopshortcut (string TargetPath, string description, String iconlocation = null, string workingdirectory = null) {if (string. IsNullOrEmpty (WorkingDirectory)) {workingdirectory = Shortcut.getdeskdir (); } return Shortcut.createshortcut (Shortcut.getdeskdir () + "\" + description + ". Lnk", TargetPath, Workingdirect Ory, description, iconlocation); }///<summary>//Create Program menu shortcuts///</summary>//<param name= "TargetPath" > available Execute file path </param>///<param name= "desCription "> Shortcut name </param>//<param name=" MenuName "> Program menu Sub-menu name, empty does not create submenu </param>//&L T;param name= "IconLocation" > Shortcut icon Path </param>//<param name= "workingdirectory" > Work path </param> <returns></returns> public static bool Createprogramsshortcut (string TargetPath, String descr Iption, String menuName, string iconlocation = null, string workingdirectory = null) {if (string. IsNullOrEmpty (WorkingDirectory)) {workingdirectory = Shortcut.getprogramsdir (); } String shortcutpath = Shortcut.getprogramsdir (); if (!string. IsNullOrEmpty (MenuName)) {Shortcutpath + = "\ \" + MenuName; if (! System.IO.Directory.Exists (Shortcutpath)) {try { System.IO.Directory.CreateDirectory (Shortcutpath); } Catch//(System.Exception ex) {return false; }}} Shortcutpath + = "\" + description + ". lnk"; Return Shortcut.createshortcut (Shortcutpath, TargetPath, WorkingDirectory, description, iconlocation); }///<summary>///Add Web page to Favorites///</summary>//<param name= "url" > Network to add to Favorites Address </param>//<param name= "description" > Title </param>//<param name= "FolderName" > Favorites File Clip name </param>//<param name= "iconlocation" > Icon file path </param>//<param name= "Workingdirec Tory "> Work path </param>//<returns></returns> public static bool Addfavorites (string URL, String description, String folderName, string iconlocation = null, string workingdirectory = null) {if (String. IsNullOrEmpty (workingdirectory)) { WorkingDirectory = Shortcut.getprogramsdir (); } String shortcutpath = Shortcut.getfavoritedir (); if (!string. IsNullOrEmpty (FolderName)) {Shortcutpath + = "\ \" + FolderName; if (! System.IO.Directory.Exists (Shortcutpath)) {try { System.IO.Directory.CreateDirectory (Shortcutpath); } catch//(System.Exception ex) {return false; }}} Shortcutpath + = "\" + description + ". lnk"; Return shortcut.createshortcut (Shortcutpath, URL, workingdirectory, description, iconlocation); }