Describes how C # adds a specified Web page to a Favorites folder

Source: Internet
Author: User
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.

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.