Enumerate and Add Internet Explorer Favourites-VBForums
Results 1 to 3 of 3

Thread: Enumerate and Add Internet Explorer Favourites

  1. #1

    Thread Starter
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,962

    Enumerate and Add Internet Explorer Favourites

    Note: VB.Net Version can be found here.

    If you have ever looked at the format of an Internet Explorer shortcut, you would know that it has a format similar to the following:

    Code:
    [DEFAULT]
    BASEURL=http://www.vbforums.com/
    [{000214A0-0000-0000-C000-000000000046}]
    Prop3=19,2
    [InternetShortcut]
    URL=http://www.vbforums.com/
    IDList=
    IconFile=http://www.vbforums.com/favicon.ico
    IconIndex=1
    i.e. it is essentially a .ini file, organized into different sections. If you ever wanted to retrieve these "Favourites" to show in your application you would need to open this file, and interrogate it for the relevant information. Thankfully, there are some PInvoke calls that can be used to make this task a little easier. This CodeBank submission aims to show how you can enumerate all your Internet Explorer Favourites and display them in your application, as well as how you can add new ones.

    Internet Explorer Favourite

    An Internet Explorer Favourite is essentially a file on your file system, with a .url extension. The name of the file, without the extenion, is used as the label for the Favourite within Internet Explorer, and the URL, contained within the file, is what is used to navigate to that Favourite. There are additional items in the .url file, including the icon to use for the shortcut, but these will not be discussed in this thread.

    PInvoke Calls

    Two PInvokes are going to be used to get/set the information that we need.

    The first is GetPrivateProfileString, you can find information about this here:

    http://www.pinvoke.net/default.aspx/...ileString.html

    This will be used to get the information regarding the URL of the shortcut.

    The second is , which you can find information about here:

    http://www.pinvoke.net/default.aspx/...ileString.html

    This will be used to create a new Internet Explorer Favourite.

    Enumerating all Internet Explorer Favourites

    All Internet Explorer Favourites are stored in the current users Favourites folder. An easy way to get to this folder is to use:

    Code:
    Environment.GetFolderPath(Environment.SpecialFolder.Favorites)
    With that in mind, the following code can be used to get all of the Favourites (including Favourites contained within nested folders) and put them into a TreeView (assuming you have a TreeView on your form called:

    Code:
            private uint result;
            private StringBuilder sb = new StringBuilder(500);
    
            private void GetFavouritesForDirectory(DirectoryInfo di, TreeNode dirNode)
            {
                foreach (FileInfo fileInfo in di.GetFiles())
                {
                    result = GetPrivateProfileString("InternetShortcut", "URL", "", sb, (uint)sb.Capacity, fileInfo.FullName);
                    if (result > 0)
                    {
                        FavouriteTreeNode myFav = new FavouriteTreeNode();
                        myFav.Text = Path.GetFileNameWithoutExtension(fileInfo.FullName);
                        myFav.Url = new Uri(sb.ToString());
                        myFav.DirectoryPath = fileInfo.FullName;
    
                        if (dirNode == null)
                        {
                            FavouritesTreeView.Nodes.Add(myFav);
                        }
                        else
                        {
                            dirNode.Nodes.Add(myFav);
                        }
                    }
                }
            }
    
            private void GetFavourites()
            {
                FavouritesTreeView.Nodes.Clear();
    
                foreach(string dirName in Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.Favorites)))
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(dirName);
                    TreeNode newNode = new TreeNode();
                    newNode.Text = dirInfo.Name;
                    newNode.Tag = dirInfo.FullName;
                    FavouritesTreeView.Nodes.Add(newNode);
                    newNode.Nodes.Add("*");
                }
    
                GetFavouritesForDirectory(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Favorites)), null);
            }
    The above code makes use of a custom class called FavouriteTreeNode. This is a simple class which inherits from TreeNode adding on a couple of specific properties to fit our needs:

    Code:
        public class FavouriteTreeNode : TreeNode
        {
            public Uri Url { get; set; }
    
            public string DirectoryPath { get; set; }
    
            public FavouriteTreeNode()
            {
            }
    
            public FavouriteTreeNode(string displayText, Uri url)
            {
                this.Text = displayText;
                this.Url = url;
            }
        }
    With these two methods defined, it is a simple matter of calling GetFavourites on the Load Event of your form.

    Due to the fact that there could literally be hundreds of Favourites stored on the user's machine, in various different nested folders, I have decided to only display the Favourites when required, rather than waste time loading them when they aren't needed. To that end, I have used the BeforeExpand event of the TreeView to decide whether or not I have to go and find some more Favourites for the currently selected Node.

    Code:
            private void FavouritesTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
            {
                if (e.Node.Nodes[0].Text == "*")
                {
                    e.Node.Nodes.Clear();
                    this.GetFavouritesForDirectory(new DirectoryInfo(e.Node.Tag.ToString()), e.Node);
                }
            }
    Add new Internet Explorer Favourite

    To add a favourite, it's a simple call to the other PInvoke call, WritePrivateProfileString, providing the information for the new path to the Favourites file, as well as the Url that you want to save with it. Here is an example:

    Code:
            private void SaveFavouriteButton_Click(object sender, EventArgs e)
            {
                if (FavouriteDisplayNameTextBox.Text == string.Empty)
                {
                    throw new ArgumentException("You must provide a Display Name for the Favourite");
                }
    
                if (FavouriteUrlTextBox.Text == string.Empty)
                {
                    throw new ArgumentException("You must provide a Url for the Favourite");
                }
    
                Uri favouriteUri;          
    
                if(!Uri.TryCreate(FavouriteUrlTextBox.Text, UriKind.RelativeOrAbsolute, out favouriteUri))
                {
                    throw new ArgumentException("Please provide a valid url");
                }
    
                string favouriteFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Favorites), FavouriteDisplayNameTextBox.Text + ".url");
    
                WritePrivateProfileString("InternetShortcut", "URL", favouriteUri.ToString(), favouriteFileName);
    
                GetFavourites();
            }
    Attached is a couple working sample of the above code. Let me know if you have any questions.
    Attached Files Attached Files

  2. #2
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,461

    Re: Enumerate and Add Internet Explorer Favourites

    Very Nice sample!

    I had to enumerate the Favourites as well, some time ago, in one of my programs, so I just did this :

    Code:
                System.Diagnostics.Process.Start("rundll32.exe", "shdocvw.dll,DoOrganizeFavDlg");
    Which just showed the Organize Favourites box. People were happy, so I was happy

    Could have used the DoOrganizeFavDlg API as well.
    There was a time, long, long a go when a guy named Hannes du Preez ( Hey! That's me!! ) knew anything and everything concerning programming technologies. Since, he has had a burn-out, got married and had kids ( and puppies - do not forget the puppies ), and our friend Hannes has turned grey, almost bald, and most importantly, his belly grew substantially! He also experienced problems with his hands.
    Now, Hannes said to himself: "Learn new things! If you do not learn new things and accept the changes in technology, you will become stuck in the same gear!"
    So, our friend, Hannes ( the unsung hero - once ) has stumbled upon the awesomeness which is mobile programming, Android, BlackBerry and Windows Phone 8. Thus far, he has done good - he even has a bunch of articles on them - with the hopes that maybe one day, he'll find his dream job.
    Besides trying to program PCs., AVR devices, and mobile devices he likes writing poetry, short stories, novels, songs, and tries his hand at art; even though his wife is a far better painter and sculptor... Moral of the story: Forgive him for not always getting it right, he is after all just human, and he has accepted his faults; but, at least he is trying! Perhaps if he would think asked questions properly through, he'd get all his answers right... Forgive him as well for being grumpy at times, as his neuro-pathways are still a bit scrambled...
    VB.NET MVP 2008 - Present

  3. #3

    Thread Starter
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,962

    Re: Enumerate and Add Internet Explorer Favourites

    Hey,

    That is an interesting approach, I hadn't considered that. May have to look into that.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.