Author:水如烟
注意:我的理解和处理结果不一定正确。
n 如何判断URL字符串是否有效
A.
命名空间:Microsoft.IE
程序集:IEHost(在 iehost.dll 中)
Manager.IsValidURL(url)
B.
<DllImport("SHLWAPI", CharSet:=CharSet.Auto)> _
Private Shared Function UrlIs(ByVal pszUrl As String, ByVal Url_Is As URL_IS) As Boolean
End Function Public Shared Function IsValidURL(ByVal pURL As String) As Boolean
Return UrlIs(pURL, URL_IS.URLIS_URL)
End Function Friend Enum URL_IS
URLIS_APPLIABLE = 4
URLIS_DIRECTORY = 5
URLIS_FILEURL = 3
URLIS_HASQUERY = 6
URLIS_NOHISTORY = 2
URLIS_OPAQUE = 1
URLIS_URL = 0
End Enum
命名空间:Microsoft.IE
程序集:IEHost(在 iehost.dll 中)
Manager.IsValidURL(url)
B.
<DllImport("SHLWAPI", CharSet:=CharSet.Auto)> _
Private Shared Function UrlIs(ByVal pszUrl As String, ByVal Url_Is As URL_IS) As Boolean
End Function Public Shared Function IsValidURL(ByVal pURL As String) As Boolean
Return UrlIs(pURL, URL_IS.URLIS_URL)
End Function Friend Enum URL_IS
URLIS_APPLIABLE = 4
URLIS_DIRECTORY = 5
URLIS_FILEURL = 3
URLIS_HASQUERY = 6
URLIS_NOHISTORY = 2
URLIS_OPAQUE = 1
URLIS_URL = 0
End Enum
n 如何预处理URL字符串
Public Shared Sub CheckUri(ByRef uri As String)
If uri Is Nothing Then uri = WebBrowserEx.DefaultUri
If uri = WebBrowserEx.DefaultUri Then Return
prefix = "file://"
Else
prefix = "http://"
End If
uri = prefix & uri
Return
End If
End Sub
注意:上面函数参数uri用ByRef定义,非ByVal
If uri Is Nothing Then uri = WebBrowserEx.DefaultUri
uri
= uri.TrimIf uri = WebBrowserEx.DefaultUri Then Return
If IsValidUrl(uri) Then Return
Dim prefix As String
If uri.StartsWith("//") OrElse Regex.IsMatch(uri, "^[A-Za-z]:") Then
prefix = "file://"
Else
prefix = "http://"
End If
If IsValidUrl(prefix & uri) Then
uri = prefix & uri
Return
End If
uri
= WebBrowserEx.DefaultUriEnd Sub
注:WebBrowserEx.DefaultUri为Const
String = "about:blank"注意:上面函数参数uri用ByRef定义,非ByVal
n 如何把URL已编码的字符串转换为解码的字符串
命名空间:System.Web
程序集:System.Web(在 system.web.dll 中)
HttpUtility.UrlDecode 方法
程序集:System.Web(在 system.web.dll 中)
HttpUtility.UrlDecode 方法
n 如何把URL字符串编码
命名空间:System.Web
程序集:System.Web(在 system.web.dll 中)
HttpUtility.UrlEncode 方法
程序集:System.Web(在 system.web.dll 中)
HttpUtility.UrlEncode 方法
n 如何取得url快捷方式的内容
Private Declare Function GetPrivateProfileStringA Lib "kernel32.dll" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String _
) As Integer Public Shared Function GetInternalShortcutUrl(ByVal filename As String) As String
Dim mResult As New System.Text.StringBuilder(" ", 260)
GetPrivateProfileStringA("InternetShortcut", "URL", "", mResult, mResult.Capacity, filename)
Return mResult.ToString
End Function Public Shared Function GetInternalShortcutIconFile(ByVal filename As String) As String
Dim mResult As New System.Text.StringBuilder(" ", 260)
GetPrivateProfileStringA("InternetShortcut", "IconFile", "", mResult, mResult.Capacity, filename)
Return mResult.ToString
End Function
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String _
) As Integer Public Shared Function GetInternalShortcutUrl(ByVal filename As String) As String
Dim mResult As New System.Text.StringBuilder(" ", 260)
GetPrivateProfileStringA("InternetShortcut", "URL", "", mResult, mResult.Capacity, filename)
Return mResult.ToString
End Function Public Shared Function GetInternalShortcutIconFile(ByVal filename As String) As String
Dim mResult As New System.Text.StringBuilder(" ", 260)
GetPrivateProfileStringA("InternetShortcut", "IconFile", "", mResult, mResult.Capacity, filename)
Return mResult.ToString
End Function
n 如何建立url快捷方式
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" ( _
ByVal lpSectionName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String _
) As Boolean Public Shared Function AddInternetShortcut(ByVal url As String, ByVal filename As String) As Boolean
filename = filename.Trim
If filename.ToLower.LastIndexOf(".url") = -1 Then filename &= ".url"
Return WritePrivateProfileString("InternetShortcut", "URL", url, filename)
End Function Public Shared Function AddInternetShortcut(ByVal url As String, ByVal path As String, ByVal name As String) As Boolean
Dim mFilename As String = My.Computer.FileSystem.CombinePath(path, name)
Return AddInternetShortcut(url, mFilename)
End Function Public Shared Function AddInternetShortcut(ByVal url As String, ByVal specialfolder As System.Environment.SpecialFolder, ByVal name As String) As Boolean
Dim mPath As String = System.Environment.GetFolderPath(specialfolder)
Return AddInternetShortcut(url, mPath, name)
End Function
ByVal lpSectionName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String _
) As Boolean Public Shared Function AddInternetShortcut(ByVal url As String, ByVal filename As String) As Boolean
filename = filename.Trim
If filename.ToLower.LastIndexOf(".url") = -1 Then filename &= ".url"
Return WritePrivateProfileString("InternetShortcut", "URL", url, filename)
End Function Public Shared Function AddInternetShortcut(ByVal url As String, ByVal path As String, ByVal name As String) As Boolean
Dim mFilename As String = My.Computer.FileSystem.CombinePath(path, name)
Return AddInternetShortcut(url, mFilename)
End Function Public Shared Function AddInternetShortcut(ByVal url As String, ByVal specialfolder As System.Environment.SpecialFolder, ByVal name As String) As Boolean
Dim mPath As String = System.Environment.GetFolderPath(specialfolder)
Return AddInternetShortcut(url, mPath, name)
End Function
n 如何读取IE缓存内容
注:参考了Scott McMaster的代码,见
WebCacheTool: Manipulate the IE Browser Cache From the Command-Line Imports System
Imports System.Runtime.InteropServices
Namespace LzmTW.uSystem.uWindows.Win32API
Friend NotInheritable Class UnsafeNativeMethods
Public Const ERROR_SUCCESS As Integer = 0
Public Const ERROR_FILE_NOT_FOUND As Integer = 2
Public Const ERROR_ACCESS_DENIED As Integer = 5
Public Const ERROR_INSUFFICIENT_BUFFER As Integer = 122
Public Const ERROR_NO_MORE_ITEMS As Integer = 259
Public Structure FILETIME
Public dwLowDateTime As Integer
Public dwHighDateTime As Integer
End Structure
Public Structure SYSTEMTIME
Public Year As Int16
Public Month As Int16
Public DayOfWeek As Int16
Public Day As Int16
Public Hour As Int16
Public Minute As Int16
Public Second As Int16
Public Milliseconds As Int16
End Structure
Public Shared Function FileTimeToSystemTime(ByRef FileTime As FILETIME, ByRef SystemTime As SYSTEMTIME) As Integer
End Function
Public Shared Function SystemTimeToTzSpecificLocalTime( _
ByVal lpTimeZoneInformation As IntPtr, _
ByRef lpUniversalTime As SYSTEMTIME, _
ByRef lpLocalTime As SYSTEMTIME _
) As Integer
End Function
If ft.dwHighDateTime = Int32.MaxValue OrElse (ft.dwLowDateTime = 0 AndAlso ft.dwHighDateTime = 0) Then
Return DateTime.MinValue
End If
Dim syst As UnsafeNativeMethods.SYSTEMTIME = New UnsafeNativeMethods.SYSTEMTIME
Dim systLocal As UnsafeNativeMethods.SYSTEMTIME = New UnsafeNativeMethods.SYSTEMTIME
If 0 = UnsafeNativeMethods.FileTimeToSystemTime(ft, syst) Then
Throw New ApplicationException("Error calling FileTimeToSystemTime: " & Marshal.GetLastWin32Error)
End If
If 0 = UnsafeNativeMethods.SystemTimeToTzSpecificLocalTime(IntPtr.Zero, syst, systLocal) Then
Throw New ApplicationException("Error calling SystemTimeToTzSpecificLocalTime: " & Marshal.GetLastWin32Error)
End If
Return New DateTime(systLocal.Year, systLocal.Month, systLocal.Day, systLocal.Hour, systLocal.Minute, systLocal.Second)
End Function
Dim dt As DateTime = FromFileTime(ft)
If dt = DateTime.MinValue Then
Return ""
End If
Return dt.ToString
End Function
End Sub
End Class
End Namespace
WebCacheTool: Manipulate the IE Browser Cache From the Command-Line Imports System
Imports System.Runtime.InteropServices
Namespace LzmTW.uSystem.uWindows.Win32API
Friend NotInheritable Class UnsafeNativeMethods
Public Const ERROR_SUCCESS As Integer = 0
Public Const ERROR_FILE_NOT_FOUND As Integer = 2
Public Const ERROR_ACCESS_DENIED As Integer = 5
Public Const ERROR_INSUFFICIENT_BUFFER As Integer = 122
Public Const ERROR_NO_MORE_ITEMS As Integer = 259
<StructLayout(LayoutKind.Sequential)> _
Public Structure FILETIME
Public dwLowDateTime As Integer
Public dwHighDateTime As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure SYSTEMTIME
Public Year As Int16
Public Month As Int16
Public DayOfWeek As Int16
Public Day As Int16
Public Hour As Int16
Public Minute As Int16
Public Second As Int16
Public Milliseconds As Int16
End Structure
<DllImport("Kernel32.dll", SetLastError:=True)> _
Public Shared Function FileTimeToSystemTime(ByRef FileTime As FILETIME, ByRef SystemTime As SYSTEMTIME) As Integer
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Public Shared Function SystemTimeToTzSpecificLocalTime( _
ByVal lpTimeZoneInformation As IntPtr, _
ByRef lpUniversalTime As SYSTEMTIME, _
ByRef lpLocalTime As SYSTEMTIME _
) As Integer
End Function
Public Shared Function FromFileTime(ByVal ft As UnsafeNativeMethods.FILETIME) As DateTime
If ft.dwHighDateTime = Int32.MaxValue OrElse (ft.dwLowDateTime = 0 AndAlso ft.dwHighDateTime = 0) Then
Return DateTime.MinValue
End If
Dim syst As UnsafeNativeMethods.SYSTEMTIME = New UnsafeNativeMethods.SYSTEMTIME
Dim systLocal As UnsafeNativeMethods.SYSTEMTIME = New UnsafeNativeMethods.SYSTEMTIME
If 0 = UnsafeNativeMethods.FileTimeToSystemTime(ft, syst) Then
Throw New ApplicationException("Error calling FileTimeToSystemTime: " & Marshal.GetLastWin32Error)
End If
If 0 = UnsafeNativeMethods.SystemTimeToTzSpecificLocalTime(IntPtr.Zero, syst, systLocal) Then
Throw New ApplicationException("Error calling SystemTimeToTzSpecificLocalTime: " & Marshal.GetLastWin32Error)
End If
Return New DateTime(systLocal.Year, systLocal.Month, systLocal.Day, systLocal.Hour, systLocal.Minute, systLocal.Second)
End Function
Public Shared Function ToStringFromFileTime(ByVal ft As UnsafeNativeMethods.FILETIME) As String
Dim dt As DateTime = FromFileTime(ft)
If dt = DateTime.MinValue Then
Return ""
End If
Return dt.ToString
End Function
Private Sub New()
End Sub
End Class
End Namespace
Imports System
Imports System.Collections
Imports System.Runtime.InteropServices
Imports System.Text.RegularExpressions
Imports System.Diagnostics Namespace LzmTW.uSystem.uWindows.Win32API
<StructLayout(LayoutKind.Sequential)> _
Public Structure INTERNET_CACHE_ENTRY_INFO
Public dwStructSize As Integer
Public lpszSourceUrlName As String
Public lpszLocalFileName As String
Public CacheEntryType As Integer
Public dwUseCount As Integer
Public dwHitRate As Integer
Public dwSizeLow As Integer
Public dwSizeHigh As Integer
Public LastModifiedTime As UnsafeNativeMethods.FILETIME
Public ExpireTime As UnsafeNativeMethods.FILETIME
Public LastAccessTime As UnsafeNativeMethods.FILETIME
Public LastSyncTime As UnsafeNativeMethods.FILETIME
Public lpHeaderInfo As IntPtr
Public dwHeaderInfoSize As Integer
Public lpszFileExtension As String
Public dwExemptDelta As Integer
End Structure
Private Shared Function FindCloseUrlCache(ByVal hEnumHandle As IntPtr) As Integer
End Function
Private Shared Function FindFirstUrlCacheEntry( _
ByVal lpszUrlSearchPattern As String, _
ByVal lpFirstCacheEntryInfo As IntPtr, _
ByRef lpdwFirstCacheEntryInfoBufferSize As Integer _
) As IntPtr
End Function
Private Shared Function FindNextUrlCacheEntry( _
ByVal hEnumHandle As IntPtr, _
ByVal lpNextCacheEntryInfo As IntPtr, _
ByRef lpdwNextCacheEntryInfoBufferSize As Integer _
) As Integer
End Function
Private Shared Function GetUrlCacheEntryInfo( _
ByVal lpszUrlName As String, _
ByVal lpCacheEntryInfo As IntPtr, _
ByRef lpdwCacheEntryInfoBufferSize As Integer _
) As Boolean
End Function
Imports System.Collections
Imports System.Runtime.InteropServices
Imports System.Text.RegularExpressions
Imports System.Diagnostics Namespace LzmTW.uSystem.uWindows.Win32API
Friend NotInheritable Class WinInetAPI
<StructLayout(LayoutKind.Sequential)> _
Public Structure INTERNET_CACHE_ENTRY_INFO
Public dwStructSize As Integer
Public lpszSourceUrlName As String
Public lpszLocalFileName As String
Public CacheEntryType As Integer
Public dwUseCount As Integer
Public dwHitRate As Integer
Public dwSizeLow As Integer
Public dwSizeHigh As Integer
Public LastModifiedTime As UnsafeNativeMethods.FILETIME
Public ExpireTime As UnsafeNativeMethods.FILETIME
Public LastAccessTime As UnsafeNativeMethods.FILETIME
Public LastSyncTime As UnsafeNativeMethods.FILETIME
Public lpHeaderInfo As IntPtr
Public dwHeaderInfoSize As Integer
Public lpszFileExtension As String
Public dwExemptDelta As Integer
End Structure
<DllImport("wininet.dll", SetLastError:=True)> _
Private Shared Function FindCloseUrlCache(ByVal hEnumHandle As IntPtr) As Integer
End Function
<DllImport("wininet.dll", SetLastError:=True)> _
Private Shared Function FindFirstUrlCacheEntry( _
ByVal lpszUrlSearchPattern As String, _
ByVal lpFirstCacheEntryInfo As IntPtr, _
ByRef lpdwFirstCacheEntryInfoBufferSize As Integer _
) As IntPtr
End Function
<DllImport("wininet.dll", SetLastError:=True)> _
Private Shared Function FindNextUrlCacheEntry( _
ByVal hEnumHandle As IntPtr, _
ByVal lpNextCacheEntryInfo As IntPtr, _
ByRef lpdwNextCacheEntryInfoBufferSize As Integer _
) As Integer
End Function
<DllImport("wininet.dll", SetLastError:=True)> _
Private Shared Function GetUrlCacheEntryInfo( _
ByVal lpszUrlName As String, _
ByVal lpCacheEntryInfo As IntPtr, _
ByRef lpdwCacheEntryInfoBufferSize As Integer _
) As Boolean
End Function
<DllImport("wininet.dll", SetLastError:=True)> _