<DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function WritePrivateProfileString(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Integer
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Clear()
Dim di As DirectoryInfo = New DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Favorites))
Dim res As Integer
Dim sb As StringBuilder
For Each fileinfo As FileInfo In di.GetFiles()
sb = New StringBuilder(500)
res = GetPrivateProfileString("InternetShortcut", "URL", "", sb, sb.Capacity, fileinfo.FullName)
If res > 0 Then
Dim myFav As New Favourite()
myFav.DisplayText = Path.GetFileNameWithoutExtension(fileinfo.FullName)
myFav.Url = New Uri(sb.ToString())
ListBox1.Items.Add(myFav)
End If
Next
End Sub
Private Sub ListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
Dim selectedFavourite As Favourite = DirectCast(ListBox1.SelectedItem, Favourite)
Me.WebBrowser1.Navigate(selectedFavourite.Url)
End Sub
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")
End If
Dim newFavourite As New Favourite()
newFavourite.DisplayText = FavouriteDisplayNameTextBox.Text
newFavourite.Url = favouriteUri
Dim favouriteFileName As String
favouriteFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Favorites), newFavourite.DisplayText + ".url")
WritePrivateProfileString("InternetShortcut", "URL", newFavourite.Url.ToString(), favouriteFileName)
ListBox1.Items.Add(newFavourite)
End Sub