📄 form1.vb
字号:
Imports VB = Microsoft.VisualBasic
Friend Class Form1
Inherits System.Windows.Forms.Form
#Region "Windows 窗体设计器生成的代码"
Public Sub New()
MyBase.New()
'此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
End Sub
'窗体重写处置,以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
If Disposing Then
If Not components Is Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(Disposing)
End Sub
Private components As System.ComponentModel.IContainer
'Windows 窗体设计器所必需的
Public ToolTip1 As System.Windows.Forms.ToolTip
Public WithEvents ListView1 As AxMSComctlLib.AxListView
Public WithEvents Label2 As System.Windows.Forms.Label
Public WithEvents Label1 As System.Windows.Forms.Label
Public WithEvents optarrange As Microsoft.VisualBasic.Compatibility.VB6.RadioButtonArray
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器来修改它。
'不要使用代码编辑器来修改它。
Public WithEvents Button3 As System.Windows.Forms.Button
Public WithEvents TextBox2 As System.Windows.Forms.TextBox
Public WithEvents TextBox1 As System.Windows.Forms.TextBox
Public WithEvents Button2 As System.Windows.Forms.Button
Public WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.ListView1 = New AxMSComctlLib.AxListView
Me.Button3 = New System.Windows.Forms.Button
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button2 = New System.Windows.Forms.Button
Me.Button1 = New System.Windows.Forms.Button
Me.Label2 = New System.Windows.Forms.Label
Me.Label1 = New System.Windows.Forms.Label
Me.optarrange = New Microsoft.VisualBasic.Compatibility.VB6.RadioButtonArray(Me.components)
CType(Me.ListView1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.optarrange, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'ListView1
'
Me.ListView1.Location = New System.Drawing.Point(5, 80)
Me.ListView1.Name = "ListView1"
Me.ListView1.OcxState = CType(resources.GetObject("ListView1.OcxState"), System.Windows.Forms.AxHost.State)
Me.ListView1.Size = New System.Drawing.Size(457, 176)
Me.ListView1.TabIndex = 7
'
'Button3
'
Me.Button3.BackColor = System.Drawing.SystemColors.Control
Me.Button3.Cursor = System.Windows.Forms.Cursors.Default
Me.Button3.Location = New System.Drawing.Point(296, 36)
Me.Button3.Name = "Button3"
Me.Button3.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Button3.Size = New System.Drawing.Size(40, 24)
Me.Button3.TabIndex = 6
Me.Button3.Text = "浏览"
'
'TextBox2
'
Me.TextBox2.AcceptsReturn = True
Me.TextBox2.AutoSize = False
Me.TextBox2.BackColor = System.Drawing.SystemColors.Window
Me.TextBox2.Cursor = System.Windows.Forms.Cursors.IBeam
Me.TextBox2.ForeColor = System.Drawing.SystemColors.WindowText
Me.TextBox2.Location = New System.Drawing.Point(110, 37)
Me.TextBox2.MaxLength = 0
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.TextBox2.Size = New System.Drawing.Size(186, 23)
Me.TextBox2.TabIndex = 5
Me.TextBox2.Text = ""
'
'TextBox1
'
Me.TextBox1.AcceptsReturn = True
Me.TextBox1.AutoSize = False
Me.TextBox1.BackColor = System.Drawing.SystemColors.Window
Me.TextBox1.Cursor = System.Windows.Forms.Cursors.IBeam
Me.TextBox1.ForeColor = System.Drawing.SystemColors.WindowText
Me.TextBox1.Location = New System.Drawing.Point(109, 8)
Me.TextBox1.MaxLength = 0
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.TextBox1.Size = New System.Drawing.Size(226, 23)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = ""
'
'Button2
'
Me.Button2.BackColor = System.Drawing.SystemColors.Control
Me.Button2.Cursor = System.Windows.Forms.Cursors.Default
Me.Button2.Location = New System.Drawing.Point(372, 38)
Me.Button2.Name = "Button2"
Me.Button2.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Button2.Size = New System.Drawing.Size(86, 31)
Me.Button2.TabIndex = 1
Me.Button2.Text = "停止"
'
'Button1
'
Me.Button1.BackColor = System.Drawing.SystemColors.Control
Me.Button1.Cursor = System.Windows.Forms.Cursors.Default
Me.Button1.Location = New System.Drawing.Point(372, 2)
Me.Button1.Name = "Button1"
Me.Button1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Button1.Size = New System.Drawing.Size(86, 31)
Me.Button1.TabIndex = 0
Me.Button1.Text = "开始查找"
'
'Label2
'
Me.Label2.BackColor = System.Drawing.SystemColors.Control
Me.Label2.Cursor = System.Windows.Forms.Cursors.Default
Me.Label2.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label2.Location = New System.Drawing.Point(16, 40)
Me.Label2.Name = "Label2"
Me.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Label2.Size = New System.Drawing.Size(91, 15)
Me.Label2.TabIndex = 4
Me.Label2.Text = "目录:"
'
'Label1
'
Me.Label1.BackColor = System.Drawing.SystemColors.Control
Me.Label1.Cursor = System.Windows.Forms.Cursors.Default
Me.Label1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label1.Location = New System.Drawing.Point(18, 13)
Me.Label1.Name = "Label1"
Me.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Label1.Size = New System.Drawing.Size(94, 18)
Me.Label1.TabIndex = 3
Me.Label1.Text = "文件名:"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.BackColor = System.Drawing.SystemColors.Control
Me.ClientSize = New System.Drawing.Size(470, 277)
Me.Controls.Add(Me.ListView1)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Cursor = System.Windows.Forms.Cursors.Default
Me.Location = New System.Drawing.Point(4, 23)
Me.Name = "Form1"
Me.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Text = "文件查询"
CType(Me.ListView1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.optarrange, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Dim findhandle As Integer
' 打开文件目录对话框函数
Private Function BrowDir() As Object
Dim bi As BROWSEINFO
Dim pidl As Object
Dim rtn As String
Dim path As String
Dim pos As Short
Dim mypath As String
' 打开目录对话框选择文件夹
pidl = SHBrowseForFolder(bi)
path = Space(512)
' 转化为文件系统路径
rtn = CStr(SHGetPathFromIDList(pidl, path))
If CBool(rtn) Then
pos = InStr(path, Chr(0))
BrowDir = VB.Left(path, pos - 1)
End If
End Function
' 开始查询
Private Sub Button1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button1.Click
FindFile((TextBox2.Text), (TextBox1.Text))
End Sub
Private Sub Button2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button2.Click
FindClose(findhandle)
End Sub
' 选择路径
Private Sub Button3_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button3.Click
TextBox2.Text = BrowDir()
End Sub
' 查找文件函数
Sub FindFile(ByRef dirpath As String, ByRef filespec As String)
Dim finddata As WIN32_FIND_DATA
Dim findnexthandle As Integer
Dim filestring As String
Dim dirfile As String
Dim n As Short
Dim fileinfo As Integer
dirpath = Trim(dirpath)
' 判断路径的有效性
If VB.Right(dirpath, 1) = "\" Then
dirfile = dirpath & filespec
Else
dirfile = dirpath & "\" & filespec
End If
' 每次查询前,清空ListView
ListView1.ListItems.Clear()
' 搜索第一个匹配的文件
findhandle = FindFirstFile(dirfile, finddata)
If findhandle <> 0 Then
' 取得查询的文件名
filestring = Trim(finddata.cFileName)
n = n + 1
' 返回文件的相关信息,
fileinfo = SHGetFileInfo(dirfile, 0, shinfo, Len(shinfo), SHGFI_TYPENAME)
' 将取得文件信息加入到ListView控件中
With ListView1.ListItems.Add(, , filestring)
.SubItems(1) = vbGetFileSizeKBStr(finddata.nFileSizeHigh + finddata.nFileSizeLow)
.SubItems(2) = vbGetFileDate(finddata.ftCreationTime)
.SubItems(3) = shinfo.szTypeName
End With
End If
If findhandle <> 0 Then
Do
System.Windows.Forms.Application.DoEvents()
' 搜索下一个匹配的文件
findnexthandle = FindNextFile(findhandle, finddata)
If VB.Right(dirpath, 1) = "\" Then
dirfile = dirpath & finddata.cFileName
Else
dirfile = dirpath & "\" & finddata.cFileName
End If
' 取得查询文件的信息
fileinfo = SHGetFileInfo(dirfile, 0, shinfo, Len(shinfo), BASIC_SHGFI_FLAGS)
If findnexthandle <> 0 Then
filestring = Trim(finddata.cFileName)
' 将查询文件的信息添加到ListView控件中
With ListView1.ListItems.Add(, , filestring)
.SubItems(1) = vbGetFileSizeKBStr(finddata.nFileSizeHigh + finddata.nFileSizeLow)
.SubItems(2) = vbGetFileDate(finddata.ftCreationTime)
.SubItems(3) = shinfo.szTypeName
End With
Else
Exit Do
End If
Loop
End If
' 查询完毕,关闭文件
Call FindClose(findhandle)
End Sub
' 程序启动,默认为C:
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "*.*"
TextBox2.Text = "C:\"
ListView1.SortKey = 0
End Sub
' 取得文件大小函数
Private Function vbGetFileSizeKBStr(ByRef fsize As Integer) As String
vbGetFileSizeKBStr = VB6.Format(((fsize) / 1000) + 0.5, "#,###,###") & "KB"
End Function
' 将文件日期转化为系统日期函数
Private Function vbGetFileDate(ByRef CT As FILETIME) As String
Dim ST As SYSTEMTIME
Dim r As Integer
Dim ds As Single
r = FileTimeToSystemTime(CT, ST)
If r Then
ds = DateSerial(ST.wYear, ST.wMonth, ST.wDay).ToOADate
vbGetFileDate = VB6.Format(ds, "long Date")
Else : vbGetFileDate = ""
End If
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -