C # Create desktop shortcuts through pure code, program menu items, add pages to Favorites

Source: Internet
Author: User
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); }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.