摘要:無論是編程愛好者,還是單位的信息管理人員,都希望以人們熟知的windows資源瀏覽器形式調取遠程,為此,本文主要介紹如何利用VB.NET和SHGetFileInfo函數實現其功能。
關鍵詞:VB.NET SHGetFileInfo 遠程資源瀏覽器
1 開發及調試的基本環境
安裝windows server 2003服務器一臺,工作站若干,其中一臺工作安裝VS2005,主要用于編寫和調試程序。
2 程序涉及對象及基本設置
首先,在VS2005環境下建立一個解決方案文件(mybrow.sln),然后創建一個窗體文件myfrm.vb,然后在窗體上按下面界面放置控件。
其主要控件設置及其功能說明如下表:
3實現各項功能的程序模塊
3.1 引用命名空間
Imports System.IO:Imports System.Text:Imports Microsoft
Imports System.Net:Imports System.runtime.interopservices
3.2 定義登錄FTP服務器,所需窗體級對象及變量
Private urib As UriBuilder:Private dlyz As NetworkCredential
Private myre As FtpWebRequest:Private pn As String
Private hn As String:Private ph As String:Private un As String
Private pd As String:Private upstr As Stream
3.3 定義連接遠程FTP服務器函數
Private Sub loginserver()
Try
urib=New UriBuilder(\"ftp\",hn,Integer.Parse(pnumber),ph)
myre=CType(FtpWebRequest.Create(urib.Uri),FtpWebRequest)
dlyz=NewNetworkCredential(un,pd):myre.Credentials=dlyz
myre.KeepAlive=False:myre.Method=\"list\"
Catch ex As WebException
MsgBox(ex.Message)
End Try
End Sub
3.4 定義獲得Ftp根目錄下文件資源函數
Private Sub getroot()
Try
Dim myre As FtpWebResponse=CType(myre.GetResponse, FtpWebResponse)
Dim mystream As Stream=myre.GetResponseStream()
Dim mystrea As StreamReader=New StreamReader(mystream, Encoding.Default)
Dim str As String=mystrea.ReadToEnd()
Dim dirfilestr() As String = Split(str, vbCrLf)
Dim dirfiletmp As String = \"\"
For Each str1 As String In dirfilestr
If VisualBasic.Left(str1, 1) = \"d\" Then
dirfiletmp += str1 vbCrLf
End If
Next
For Each str1 As String In dirfilestr
If VisualBasic.Left(str1, 1) <> \"d\" Then
dirfiletmp += str1 vbCrLf
End If
Next
dirfilestr = dirfiletmp.Split(vbCrLf)
With List1
.Items.Clear()
Dim i As Integer = 0
For Each str1 As String In dirfilestr
If str1.Trim.Length = 0 Then Exit For
Dim FDname as string = str1.Substring(55, str1.Length - 55)
Dim FDlng as string = str1.Substring(31, 12)
Dim FDdate as string = str1.Substring(43, 12)
Dim FDxx as string = str1.Substring(0, 10).Trim
If FDname.Trim <> \".\" And FDname.Trim <> \"..\" Then
If VisualBasic.Left(FDxx, 1) = \"d\" Then
Dim it As ListViewItem:it = .Items.Add(FDname)
.Items(i).ImageKey = \"dir\":it.SubItems.Add(\"文件夾\")
it.SubItems.Add(FDlng):t.SubItems.Add(FDdate)
Else
geticon(FDname, FDlng, FDdate)
End If
i = i + 1
End If
Next
End With
Catch ex As WebException
MsgBox(ex.Message)
End Try
End Sub
3.5 定義獲取資源類型圖標函數
Private Sub geticon(ByVal fn As String, ByVal flng As String, ByVal fdate As String)
Dim ext As String = Path.GetExtension(fn)
Dim ico As Icon:Dim shfi_small As New shfileinfo
Im1.ImageSize = New System.Drawing.Size(16, 16)
Try
Dim Ltem As ListViewItem
SHGetFileInfo(ext,0,shfi_small, Marshal.SizeOf(shfi_small), SHGFI_SMALLICON Or shgfi_icon Or shgfi_usefileattributes)
ico = Icon.FromHandle(shfi_small.hicon)
Dim keyindex As Integer = Im1.Images.IndexOfKey(ext)
If keyindex<0 Then
Im1.Images.Add(ico):keyindex=Im1.Images.Count-1
Im1.Images.SetKeyName(keyindex, ext)
End If
Ltem = New ListViewItem(fn):LstItem.ImageIndex=keyindex
List1.Items.Add(Ltem):LstItem.SubItems.Add(\"文件\")
Dim filels As Long=flng \\ 1024
Ltem.SubItems.Add(filels.ToString + \"KB\")
Ltem.SubItems.Add(fdate)
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
4 登錄遠程服務器,并獲取文件資源
在登錄按鈕的單擊事件中,編寫如下語句。
Private Sub B1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B1.Click
hn = T1.Text:pn = “21”
un = T2.Text:pd = T3.Text
ph = \"\":loginserver():getroot()
End Sub
上述,即為實現其功能代碼,愛好者稍加修改即可完成基于網絡的資源上傳和下載功能,同時敬請各位同行批評指正。