Results 1 to 1 of 1

Thread: Enumerate and Add Internet Explorer Favourites

  1. #1

    Thread Starter
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    The Granite City

    Enumerate and Add Internet Explorer Favourites

    Note: C# 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:

    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:

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

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

    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:

    vb Code:
    1. 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:

    vb Code:
    1. Private sb As New StringBuilder(500)
    2.     Private result As Integer
    4.     Private Sub GetFavouritesForDirectory(ByVal di As DirectoryInfo, ByVal dirNode As TreeNode)
    5.         For Each fileinfo As FileInfo In di.GetFiles()
    6.             result = GetPrivateProfileString("InternetShortcut", "URL", "", sb, sb.Capacity, fileinfo.FullName)
    7.             If result > 0 Then
    8.                 Dim myFav As New FavouriteTreeNode()
    9.                 myFav.Text = Path.GetFileNameWithoutExtension(fileinfo.FullName)
    10.                 myFav.Url = New Uri(sb.ToString())
    11.                 myFav.DirectoryPath = fileinfo.FullName
    12.                 If dirNode Is Nothing Then
    13.                     FavouritesTreeView.Nodes.Add(myFav)
    14.                 Else
    15.                     dirNode.Nodes.Add(myFav)
    16.                 End If
    17.             End If
    18.         Next
    19.     End Sub
    21.     Private Sub GetFavourites()
    22.         FavouritesTreeView.Nodes.Clear()
    24.         For Each dirName As String In Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.Favorites))
    25.             Dim dirInfo As New DirectoryInfo(dirName)
    26.             Dim NewNode As New TreeNode()
    27.             NewNode.Text = dirInfo.Name
    28.             NewNode.Tag = dirInfo.FullName
    29.             FavouritesTreeView.Nodes.Add(NewNode)
    30.             NewNode.Nodes.Add("*")
    31.         Next
    33.         GetFavouritesForDirectory(New DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Favorites)), Nothing)
    34.     End Sub

    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:

    vb Code:
    1. Public Class FavouriteTreeNode
    2.     Inherits TreeNode
    4.     Private _url As Uri
    5.     Public Property Url() As Uri
    6.         Get
    7.             Return _url
    8.         End Get
    9.         Set(ByVal value As Uri)
    10.             _url = value
    11.         End Set
    12.     End Property
    14.     Private _directoryPath As String
    15.     Public Property DirectoryPath() As String
    16.         Get
    17.             Return _directoryPath
    18.         End Get
    19.         Set(ByVal value As String)
    20.             _directoryPath = value
    21.         End Set
    22.     End Property
    24.     Public Sub New()
    26.     End Sub
    28.     Public Sub New(ByVal displayText As String, ByVal url As Uri)
    29.         Me.Text = displayText
    30.         Me.Url = url
    31.     End Sub
    32. End Class

    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.

    vb Code:
    1. Private Sub FavouritesTreeView_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles FavouritesTreeView.BeforeExpand
    2.         If e.Node.Nodes(0).Text = "*" Then
    3.             e.Node.Nodes.Clear()
    4.             Me.GetFavouritesForDirectory(New DirectoryInfo(e.Node.Tag.ToString()), e.Node)
    5.         End If
    6.     End Sub

    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:

    vb Code:
    1. Private Sub SaveFavouriteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFavouriteButton.Click
    2.         If FavouriteDisplayNameTextBox.Text = String.Empty Then
    3.             Throw New ArgumentException("You must provide a Display Name for the Favourite")
    4.         End If
    6.         If FavouriteUrlTextBox.Text = String.Empty Then
    7.             Throw New ArgumentException("You must provide a Url for the Favourite")
    8.         End If
    10.         Dim favouriteUri As Uri
    12.         If Not Uri.TryCreate(FavouriteUrlTextBox.Text, UriKind.RelativeOrAbsolute, favouriteUri) Then
    13.             Throw New ArgumentException("Please provide a valid Url")
    14.         End If
    16.         Dim favouriteFileName As String
    17.         favouriteFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Favorites), FavouriteDisplayNameTextBox.Text + ".url")
    19.         WritePrivateProfileString("InternetShortcut", "URL", favouriteUri.ToString(), favouriteFileName)
    21.         GetFavourites()
    22.     End Sub

    Attached is a couple working sample of the above code. Let me know if you have any questions.
    Attached Files Attached Files

Posting Permissions

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

Click Here to Expand Forum to Full Width