VB2010(22)查看收藏页
界面
后台
Public Class Viewer
Private Sub Viewer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Using objfavorites As New Favorites
objfavorites.ScanFavorites()
For Each objwebfavorite As WebFavorite In objfavorites.FavoritesCollection
Dim objListViewItem As New ListViewItem
objListViewItem.Text = objwebfavorite.Name
objListViewItem.SubItems.Add(objwebfavorite.Url)
lvwFavorites.Items.Add(objListViewItem)
Next
End Using
Catch ExceptionErr As Exception
MessageBox.Show(ExceptionErr.Message, "favorites Viewer", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
End Sub
Private Sub lvwFavorites_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwFavorites.Click
lnkUrl.Text = "visit " & lvwFavorites.SelectedItems.Item(0).Text
lnkUrl.Links.Clear()
lnkUrl.Links.Add(6, lvwFavorites.SelectedItems.Item(0).Text.Length, lvwFavorites.SelectedItems.Item(0).SubItems(1).Text)
End Sub
Private Sub lnkUrl_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkUrl.LinkClicked
Process.Start(e.Link.LinkData)
End Sub
End Class
后台逻辑:三个类
一、'利用收藏页创建列表项对象
Imports System.IO
Public Class WebFavorite
Implements IDisposable
#Region "IDisposable Support"
Private disposedValue As Boolean ' 检测冗余的调用
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: 释放托管状态(托管对象)。
End If
' TODO: 释放非托管资源(非托管对象)并重写下面的 Finalize()。
' TODO: 将大型字段设置为 null。
End If
Me.disposedValue = True
End Sub
' TODO: 仅当上面的 Dispose(ByVal disposing As Boolean)具有释放非托管资源的代码时重写 Finalize()。
'Protected Overrides Sub Finalize()
' ' 不要更改此代码。请将清理代码放入上面的 Dispose(ByVal disposing As Boolean)中。
' Dispose(False)
' MyBase.Finalize()
'End Sub
' Visual Basic 添加此代码是为了正确实现可处置模式。
Public Sub Dispose() Implements IDisposable.Dispose
' 不要更改此代码。请将清理代码放入上面的 Dispose(ByVal disposing As Boolean)中。
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
'一个收藏页文件的内容大致是这个样子的
'[DEFAULT]
'BASEURL=http://www.google.com/
'[InternetShotcut]
'URL=http://www.google.com/
'IDList=
'IconFile=http://www.google.com/favicon.ico
'IconInext=1
'[(000214A0-0000-0000-c120-0000000046)]
'Prop3=19,2
'设置为收藏页的快捷文件的文件名
Public Name As String
'设置找到的URL
Public Url As String
Public Sub Load(ByVal fileName As String) 'filename为收藏页的快捷文件的完整路径
'接收收藏页的快捷文件的完整内容
Dim strData As String
'创建包含StrData变量中各行的数据的数组
Dim strLines() As String
'迭代各行的数组,以找出URL
Dim strLine As String
'获取传递给Load方法的完整路径和文件名的信息
Dim objFileInfo As New FileInfo(fileName) 'FileInfo返回文件的完整路径。
'将Name设置为收藏页的快捷文件的文件名,不包括路径和扩展名
Name = objFileInfo.Name.Substring(0, objFileInfo.Name.Length - objFileInfo.Extension.Length)
Try
'读入filename的完整内容
strData = My.Computer.FileSystem.ReadAllText(fileName)
'按行分割strData,Crlf代表回车和换行符,RemoveEmptyEntries指删除空行
strLines = strData.Split(New String() {ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries)
For Each strLine In strLines
If strLine.StartsWith("URL=") Then
Url = strLine.Substring(4)
Exit For
End If
Next
Catch IOexceptionErr As IOException
Throw New Exception(IOexceptionErr.Message)
End Try
End Sub
End Class
二、
'遍历收藏页,保存为WebFavorite集合
Imports System.Collections
'实例化为一个对象以保存大量的WebFavorite
Public Class WebFavoriteCollection
Inherits CollectionBase
Public Sub Add(ByVal Favorite As WebFavorite)
'List是Collection的一个受保护的成员
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文件夹,创建新的WebFavorite对象,并添加到集合中
'遍历Favorite文件夹,返回一个WebfavoriteCollection对象,为文件夹中的每个收藏页包含一个WebFavorite对象
Public Class Favorites
Implements IDisposable
#Region "IDisposable Support"
Private disposedValue As Boolean ' 检测冗余的调用
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: 释放托管状态(托管对象)。
End If
' TODO: 释放非托管资源(非托管对象)并重写下面的 Finalize()。
' TODO: 将大型字段设置为 null。
End If
Me.disposedValue = True
End Sub
' TODO: 仅当上面的 Dispose(ByVal disposing As Boolean)具有释放非托管资源的代码时重写 Finalize()。
'Protected Overrides Sub Finalize()
' ' 不要更改此代码。请将清理代码放入上面的 Dispose(ByVal disposing As Boolean)中。
' Dispose(False)
' MyBase.Finalize()
'End Sub
' Visual Basic 添加此代码是为了正确实现可处置模式。
Public Sub Dispose() Implements IDisposable.Dispose
' 不要更改此代码。请将清理代码放入上面的 Dispose(ByVal disposing As Boolean)中。
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
'列表集合
Public FavoritesCollection As New WebFavoriteCollection
'获取Favorite文件夹
Public ReadOnly Property FavoritesFolder As String
Get
Return Environment.GetFolderPath(Environment.SpecialFolder.Favorites)
End Get
End Property
'生成Collection
Public Sub ScanFavorites()
ScanFavorites(FavoritesFolder)
End Sub
'重载生成Collection
Public Sub ScanFavorites(ByVal FolderName As String)
'遍历文件夹中的文件
For Each strfile As String In (My.Computer.FileSystem.GetFiles(FolderName))
If strfile.EndsWith(".url", True, Nothing) Then
Try
'生成Webfavorte对象
Dim objWebFavorite As New WebFavorite
objWebFavorite.Load(strfile)
'添加到Collection集合
FavoritesCollection.Add(objWebFavorite)
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End If
Next
End Sub
End Class