Yesterday I wanted to implement a small function, that is, a Web page you are browsing to add to the Favorites folder. I used to call a method on the page directly with JavaScript, and now I want to use WinForm to achieve, I did not see the relevant methods of the method (may not be careful to find). I thought about it and decided to make it.
To complete this function is mainly two steps, first to obtain the system user's Favorites directory, the second is to obtain the page address in the Favorites directory to create a shortcut.
To get a list of favorites we can use the GetFolderPath method to complete the code as follows
private void Button1_Click (object sender, EventArgs e) { //string path=environment.getfolderpath ( System.Environment.SpecialFolder.Favorites); To create a shortcut, you need to use the Iwshruntimelibrary namespace, where we need a using. //And add a Com,windows script Host object model to the reference. addfavorites ("Http://blog.csdn.net/testcs_dn", "Beginner VC record Bit"); MessageBox.Show ("added successfully! "); }
To create a shortcut, you need to use the Iwshruntimelibrary namespace, where we need a using. and add a Com,windows script Host object model to the reference. Add to Favorites method as follows "
<summary>//////Add to Favorites method///</summary>//<param name= "url" ></param> <param name= "filename" ></param>//<param name= "Savepath" ></param> public void Addfavorites (String url,string filename,string Savepath = null) {if (string. Isnullorwhitespace (Savepath)) {Savepath = "Favorites"; } string Path=environment.getfolderpath (System.Environment.SpecialFolder.Favorites); if (! System.IO.File.Exists (path+ "\ \" +filename+savepath+ ". url")) {Iwshshell_class sh ell = new Iwshshell_classclass (); Iwshurlshortcut Shortcut=null; if (savepath== "Favorites") {shortcut = Shell. CreateShortcut (Environment.getfolderpath (System.Environment.SpecialFolder.Favorites) + "\ \" +filename+ ". url") As Iwshurlshortcut; } else {shortcut = Shell. CreateShortcut (Environment.getfolderpath (System.Environment.SpecialFolder.Favorites) + "\ \" +savepath+ "\ \" + filename+ ". url") as Iwshurlshortcut; } shortcut. TargetPath = URL; Shortcut. Save (); } }
Where the URL is the path where you want to save the page, filename is the name of the generated shortcut, and Savepath is the folder in which you saved it.
Effect:
By the way, ask a question, who knows how to get the Axwebbrowser object in StatusText.