一、首先建一个单个书签的类WebFavorite
Code
1 Public Name As String '定义书签类属性--名称
2 Public Url As String '定义书签类属性--网址
3 Public Sub Load()Sub Load(ByVal filename As String) '书签类Load方法 参数表示书签文件名如:书签名.url
4
5 Dim strData As String
6 Dim strLines() As String
7 Dim strLine As String
8 Dim objFileInfo As New FileInfo(filename)
9 Name = objFileInfo.Name.Substring(0, objFileInfo.Name.Length - objFileInfo.Extension.Length)
10 Try
11 strData = My.Computer.FileSystem.ReadAllText(filename) '读取文本文件
12 strLines = strData.Split(New String() {ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries) '截取每行组成字符串数组
13
14 For Each strLine In strLines '取每行数据
15 If strLine.StartsWith("URL=") Then
16 Url = strLine.Substring(4)
17 Exit For
18 End If
19 Next
20
21 Catch IOExceptionErr As IOException
22 Throw New Exception(IOExceptionErr.Message)
23
24 End Try
25
26 End Sub
1 Public Name As String '定义书签类属性--名称
2 Public Url As String '定义书签类属性--网址
3 Public Sub Load()Sub Load(ByVal filename As String) '书签类Load方法 参数表示书签文件名如:书签名.url
4
5 Dim strData As String
6 Dim strLines() As String
7 Dim strLine As String
8 Dim objFileInfo As New FileInfo(filename)
9 Name = objFileInfo.Name.Substring(0, objFileInfo.Name.Length - objFileInfo.Extension.Length)
10 Try
11 strData = My.Computer.FileSystem.ReadAllText(filename) '读取文本文件
12 strLines = strData.Split(New String() {ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries) '截取每行组成字符串数组
13
14 For Each strLine In strLines '取每行数据
15 If strLine.StartsWith("URL=") Then
16 Url = strLine.Substring(4)
17 Exit For
18 End If
19 Next
20
21 Catch IOExceptionErr As IOException
22 Throw New Exception(IOExceptionErr.Message)
23
24 End Try
25
26 End Sub
二、建书签集合的类WebFavoriteCollection
Code
Public Class WebFavoriteCollection
Inherits CollectionBase
Public Sub Add(ByVal Favorite As WebFavorite)
List.Add(Favorite)
End Sub
Public Sub Remove(ByVal Index As Integer)
If Index > 0 And Index < Count Then
List.Remove(Index)
End If
End Sub
Public ReadOnly Property Item(ByVal Index As Integer) As WebFavorite
Get
Return CType(List.Item(Index), WebFavorite)
End Get
End Property
End Class
Public Class WebFavoriteCollection
Inherits CollectionBase
Public Sub Add(ByVal Favorite As WebFavorite)
List.Add(Favorite)
End Sub
Public Sub Remove(ByVal Index As Integer)
If Index > 0 And Index < Count Then
List.Remove(Index)
End If
End Sub
Public ReadOnly Property Item(ByVal Index As Integer) As WebFavorite
Get
Return CType(List.Item(Index), WebFavorite)
End Get
End Property
End Class
三、建Favorites类获取收藏夹数据
Code
Public FavoritesCollection As WebFavoriteCollection
Public ReadOnly Property FavoritesFolder() As String
Get
Return Environment.GetFolderPath(Environment.SpecialFolder.Favorites) '返回系统收收藏夹存储的位置
End Get
End Property
Public Sub ScanFavorites()
ScanFavorites(FavoritesFolder)
End Sub
Public Sub ScanFavorites(ByVal folderName As String)
If FavoritesCollection Is Nothing Then
FavoritesCollection = New WebFavoriteCollection
End If
For Each strFile As String In My.Computer.FileSystem.GetFiles(folderName)
My.Computer.FileSystem.GetFiles(folderName)
If strFile.EndsWith(".url", True, Nothing) Then
Try
Using objWebFavorite As New WebFavorite '用Using确保系统资源及时回收
objWebFavorite.Load(strFile)
FavoritesCollection.Add(objWebFavorite)
End Using
Catch ExceptionErr As Exception
Throw New Exception(ExceptionErr.Message)
End Try
End If
Next
End Sub
Public FavoritesCollection As WebFavoriteCollection
Public ReadOnly Property FavoritesFolder() As String
Get
Return Environment.GetFolderPath(Environment.SpecialFolder.Favorites) '返回系统收收藏夹存储的位置
End Get
End Property
Public Sub ScanFavorites()
ScanFavorites(FavoritesFolder)
End Sub
Public Sub ScanFavorites(ByVal folderName As String)
If FavoritesCollection Is Nothing Then
FavoritesCollection = New WebFavoriteCollection
End If
For Each strFile As String In My.Computer.FileSystem.GetFiles(folderName)
My.Computer.FileSystem.GetFiles(folderName)
If strFile.EndsWith(".url", True, Nothing) Then
Try
Using objWebFavorite As New WebFavorite '用Using确保系统资源及时回收
objWebFavorite.Load(strFile)
FavoritesCollection.Add(objWebFavorite)
End Using
Catch ExceptionErr As Exception
Throw New Exception(ExceptionErr.Message)
End Try
End If
Next
End Sub
有了以上三个类我们就可以在程序窗体中调用
下载源码:/Files/quejuwen/Favorites.rar
使用现有的三个类,我们还可以建立任务栏程序如: