VB2008写的--收藏夹查看程序,适合.net初学者研究(附源码)

一、首先建一个单个书签的类WebFavorite
1
Public Name As String '定义书签类属性--名称2
Public Url As String '定义书签类属性--网址3

Public Sub Load()Sub Load(ByVal filename As String) '书签类Load方法 参数表示书签文件名如:书签名.url4

5
Dim strData As String6
Dim strLines() As String7
Dim strLine As String8
Dim objFileInfo As New FileInfo(filename)9
Name = objFileInfo.Name.Substring(0, objFileInfo.Name.Length - objFileInfo.Extension.Length)10
Try11
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=") Then16
Url = strLine.Substring(4)17
Exit For18
End If19
Next20

21
Catch IOExceptionErr As IOException22
Throw New Exception(IOExceptionErr.Message)23

24
End Try25

26
End Sub
二、建书签集合的类WebFavoriteCollection
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类获取收藏夹数据
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
使用现有的三个类,我们还可以建立任务栏程序如:


浙公网安备 33010602011771号