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:
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:
Private sb As New StringBuilder(500)
Private result As Integer
Private Sub GetFavouritesForDirectory(ByVal di As DirectoryInfo, ByVal dirNode As TreeNode)
For Each fileinfo As FileInfo In di.GetFiles()
result = GetPrivateProfileString("InternetShortcut", "URL", "", sb, sb.Capacity, fileinfo.FullName)
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:
Public Class FavouriteTreeNode
Inherits TreeNode
Private _url As Uri
Public Property Url() As Uri
Get
Return _url
End Get
Set(ByVal value As Uri)
_url = value
End Set
End Property
Private _directoryPath As String
Public Property DirectoryPath() As String
Get
Return _directoryPath
End Get
Set(ByVal value As String)
_directoryPath = value
End Set
End Property
Public Sub New()
End Sub
Public Sub New(ByVal displayText As String, ByVal url As Uri)
Me.Text = displayText
Me.Url = url
End Sub
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:
Private Sub FavouritesTreeView_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles FavouritesTreeView.BeforeExpand
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:
Private Sub SaveFavouriteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFavouriteButton.Click
If FavouriteDisplayNameTextBox.Text = String.Empty Then
Throw New ArgumentException("You must provide a Display Name for the Favourite")
End If
If FavouriteUrlTextBox.Text = String.Empty Then
Throw New ArgumentException("You must provide a Url for the Favourite")
End If
Dim favouriteUri As Uri
If Not Uri.TryCreate(FavouriteUrlTextBox.Text, UriKind.RelativeOrAbsolute, favouriteUri) Then
Throw New ArgumentException("Please provide a valid Url")