3个回答
展开全部
新建一个项目,将原来form1的所有代码删了,将下面代码粘贴上去,就可以了。
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
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
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(56, 128)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(304, 21)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(416, 389)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.AllowDrop = True
listview1.AllowDrop = True
listview1.MultiSelect = True
listview1.FullRowSelect = True
listview1.View = View.Details
' 声明并构建 ColumnHeader 对象。
Dim header1, header2, header3, header4 As ColumnHeader
header1 = New ColumnHeader
header2 = New ColumnHeader
header3 = New ColumnHeader
header4 = New ColumnHeader
' 设定资料行的行首文字、水平对齐、以及宽度。
header1.Text = "文件名称"
header1.TextAlign = HorizontalAlignment.Left
header1.Width = 200
' 设定资料行的行首文字、水平对齐、以及宽度。
header2.TextAlign = HorizontalAlignment.Left
header2.Text = "所在文件夹"
header2.Width = 200
' 设定资料行的行首文字、水平对齐、以及宽度。
header3.TextAlign = HorizontalAlignment.Left
header3.Text = "大小"
header3.Width = 150
' 设定资料行的行首文字、水平对齐、以及宽度。
header4.TextAlign = HorizontalAlignment.Left
header4.Text = "修改日期"
header4.Width = 150
' 将资料行行首加入 ListView 控制项中。
listview1.Columns.Add(header1)
listview1.Columns.Add(header2)
listview1.Columns.Add(header3)
listview1.Columns.Add(header4)
End Sub
Dim listview1 As New ListView
Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim MyFiles(), MyFilesName(100), MyFilesDir As String
Dim i, j As Integer
' 将文件指派给一个数组。
MyFiles = e.Data.GetData(DataFormats.FileDrop)
MyFilesDir = Strings.Left(MyFiles(0), Strings.InStrRev(MyFiles(0), "\"))
ReDim MyFilesName(MyFiles.Length) '至关重要的一步
For i = 0 To MyFiles.Length - 1
MyFilesName(i) = Strings.Right(MyFiles(i), Strings.Len(MyFiles(i)) - Strings.InStrRev(MyFiles(i), "\"))
Next
Dim dirInfo As New System.IO.DirectoryInfo(MyFilesDir)
Dim fileSysInfos(), filesysinfo As System.IO.FileSystemInfo
Dim file As System.IO.FileInfo
Try
fileSysInfos = dirInfo.GetFileSystemInfos
ReDim fileSysInfos(MyFiles.Length) '至关重要的一步
Dim fileVers(fileSysInfos.Length), fileVer As FileVersionInfo
For i = 0 To MyFiles.Length - 1
fileSysInfos(i) = dirInfo.GetFileSystemInfos(MyFilesName(i))(0)
If Not CBool(fileSysInfos(i).Attributes And IO.FileAttributes.Directory) Then
fileVers(i) = FileVersionInfo.GetVersionInfo(MyFiles(i))
End If
Next
For Each filesysinfo In fileSysInfos
If Not filesysinfo Is Nothing Then '这一句可以避免“未将对象引用设置到对象的实例”(也就是空引用)的错误
j = listview1.Items.Count
listview1.Items.Add(filesysinfo.Name)
listview1.Items(j).SubItems.Add(MyFilesDir)
If CBool(filesysinfo.Attributes And IO.FileAttributes.Directory) Then
listview1.Items(j).SubItems.Add("")
listview1.Items(j).SubItems.Add("文件夹")
Else
file = dirInfo.GetFiles(filesysinfo.Name.ToString)(0)
fileVer = FileVersionInfo.GetVersionInfo(MyFilesDir & "\" & filesysinfo.Name)
listview1.Items(j).SubItems.Add(file.Length)
If fileVer.FileDescription = "" Then
listview1.Items(j).SubItems.Add(file.Extension)
'MsgBox(file.Extension)
If file.Extension = ".txt" Then
TextBox1.Text = MyFilesDir & filesysinfo.Name
Else
TextBox1.Text = "你拖动的不是文本文档"
End If
Else
listview1.Items(j).SubItems.Add(fileVer.FileDescription)
End If
End If
listview1.Items(j).SubItems.Add(filesysinfo.CreationTime)
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
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
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(56, 128)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(304, 21)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(416, 389)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.AllowDrop = True
listview1.AllowDrop = True
listview1.MultiSelect = True
listview1.FullRowSelect = True
listview1.View = View.Details
' 声明并构建 ColumnHeader 对象。
Dim header1, header2, header3, header4 As ColumnHeader
header1 = New ColumnHeader
header2 = New ColumnHeader
header3 = New ColumnHeader
header4 = New ColumnHeader
' 设定资料行的行首文字、水平对齐、以及宽度。
header1.Text = "文件名称"
header1.TextAlign = HorizontalAlignment.Left
header1.Width = 200
' 设定资料行的行首文字、水平对齐、以及宽度。
header2.TextAlign = HorizontalAlignment.Left
header2.Text = "所在文件夹"
header2.Width = 200
' 设定资料行的行首文字、水平对齐、以及宽度。
header3.TextAlign = HorizontalAlignment.Left
header3.Text = "大小"
header3.Width = 150
' 设定资料行的行首文字、水平对齐、以及宽度。
header4.TextAlign = HorizontalAlignment.Left
header4.Text = "修改日期"
header4.Width = 150
' 将资料行行首加入 ListView 控制项中。
listview1.Columns.Add(header1)
listview1.Columns.Add(header2)
listview1.Columns.Add(header3)
listview1.Columns.Add(header4)
End Sub
Dim listview1 As New ListView
Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim MyFiles(), MyFilesName(100), MyFilesDir As String
Dim i, j As Integer
' 将文件指派给一个数组。
MyFiles = e.Data.GetData(DataFormats.FileDrop)
MyFilesDir = Strings.Left(MyFiles(0), Strings.InStrRev(MyFiles(0), "\"))
ReDim MyFilesName(MyFiles.Length) '至关重要的一步
For i = 0 To MyFiles.Length - 1
MyFilesName(i) = Strings.Right(MyFiles(i), Strings.Len(MyFiles(i)) - Strings.InStrRev(MyFiles(i), "\"))
Next
Dim dirInfo As New System.IO.DirectoryInfo(MyFilesDir)
Dim fileSysInfos(), filesysinfo As System.IO.FileSystemInfo
Dim file As System.IO.FileInfo
Try
fileSysInfos = dirInfo.GetFileSystemInfos
ReDim fileSysInfos(MyFiles.Length) '至关重要的一步
Dim fileVers(fileSysInfos.Length), fileVer As FileVersionInfo
For i = 0 To MyFiles.Length - 1
fileSysInfos(i) = dirInfo.GetFileSystemInfos(MyFilesName(i))(0)
If Not CBool(fileSysInfos(i).Attributes And IO.FileAttributes.Directory) Then
fileVers(i) = FileVersionInfo.GetVersionInfo(MyFiles(i))
End If
Next
For Each filesysinfo In fileSysInfos
If Not filesysinfo Is Nothing Then '这一句可以避免“未将对象引用设置到对象的实例”(也就是空引用)的错误
j = listview1.Items.Count
listview1.Items.Add(filesysinfo.Name)
listview1.Items(j).SubItems.Add(MyFilesDir)
If CBool(filesysinfo.Attributes And IO.FileAttributes.Directory) Then
listview1.Items(j).SubItems.Add("")
listview1.Items(j).SubItems.Add("文件夹")
Else
file = dirInfo.GetFiles(filesysinfo.Name.ToString)(0)
fileVer = FileVersionInfo.GetVersionInfo(MyFilesDir & "\" & filesysinfo.Name)
listview1.Items(j).SubItems.Add(file.Length)
If fileVer.FileDescription = "" Then
listview1.Items(j).SubItems.Add(file.Extension)
'MsgBox(file.Extension)
If file.Extension = ".txt" Then
TextBox1.Text = MyFilesDir & filesysinfo.Name
Else
TextBox1.Text = "你拖动的不是文本文档"
End If
Else
listview1.Items(j).SubItems.Add(fileVer.FileDescription)
End If
End If
listview1.Items(j).SubItems.Add(filesysinfo.CreationTime)
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
End Class
参考资料: http://hi.baidu.com/harryglory/blog/item/e121dafe045fbc355d600837.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询