Today I will introduce a small function, which is to add a certain web page you are browsing to your favorites. There are two main steps to complete this function. First, you need to obtain the favorites directory of the system user, and the second is to create a shortcut in the favorites directory based on the page address. Let’s learn more about it together.
1. C# creates shortcuts
To create a shortcut, you must reference it. The reference method is: Add a reference to the project ->Select COM component ->Select "Windows Script Host Object Model" to confirm, and the addition is successful! Next is encoding:
/// <summary> /// Generate shortcuts/// </summary> /// <param name="targetPath">Original target location</param>/// /// <param name="savePath">Location of the save shortcut</param>protected void CreateShortcuts(String targetPath, String savePath,String saveName) { shell_class = new IWshRuntimeLibrary.IWshShell_ClassClass(); shortcut = null; if (!(targetPath)) return; if (!Directory(savePath)) (savePath); try { shortcut = shell_class.CreateShortcut(savePath + @"/" + saveName + ".lnk") as ; = targetPath; (); ("Chuangjia shortcut succeeded!"); } catch (Exception ex) { ("Chuangjia shortcut failed!"); } }
The above is the method of calling the corresponding method in C# to create shortcuts; next thing we want to talk about is adding a web page to a favorite in C#. In fact, the essence of adding a web page to a favorite is to generate a shortcut for the given web page and place it in the physical folder of the computer corresponding to the favorites.
2. Add web pages to favorites
First, refer to the corresponding dll as in the first step
/// <summary> /// Add favorites/// </summary> /// <param name="url">the corresponding web page url</param>/// <param name="saveName">Save name</param>/// <param name="folderName">Folder name</param>protected void AddToFavorites(String url, String saveName, String folderName) { request = ()(new Uri(url)); = "GET"; = 10000; try { response = ()(); if ( == ) { //Get the physical folder location of the current user's favorites String favoritesPath = (); String savePath = favoritesPath; if (!(folderName)) { savePath += @"/" + folderName; if (!(savePath)) (savePath); } shell_class = new (); shortcut = null; try { shortcut = shell_class.CreateShortcut(favoritesPath + @"/" + saveName + ".lnk") as ; = url; (); ("Added successfully"); } catch (Exception ex) { ("Add failed"); } } else { ("Request failed"); } } catch (Exception ex) { (); } }
I hope this article will be helpful to you. This is the only example content of C# implementation of creating shortcuts and adding web pages to favorites. I hope everyone will continue to follow our website! If you want to learn C#, you can continue to follow this website.