您的位置:首页 > 编程语言 > VB

vb.net 文件夹实现移动 并显示进度条,richtextbox1显示移动的文件名

2011-01-28 00:06 387 查看
利用线程,和计时器,Function GetFolderSize从网上借鉴的,IO类只支持不同分区移动,所以移动目录只能在相同分区,设计界面如图,

如: d:/a d:/b(空目录)

代码如下

Imports System.IO
Imports System.Threading

Public Class Form2
Dim SourceDir, DestDir As String
Dim SourceLen As Integer

'返回文件夹大小
Private Function GetFolderSize(ByVal DirPath As String, Optional ByVal IncludeSubFolders As Boolean = True) As Long
Dim lngDirSize As Long
Dim objFileInfo As FileInfo
Dim objDir As DirectoryInfo = New DirectoryInfo(DirPath)
Dim objSubFolder As DirectoryInfo
Try
For Each objFileInfo In objDir.GetFiles()
lngDirSize += objFileInfo.Length
Next
If IncludeSubFolders Then
For Each objSubFolder In objDir.GetDirectories()
lngDirSize += GetFolderSize(objSubFolder.FullName)
Next
End If
Catch
End Try
Return lngDirSize
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim T As New Thread(AddressOf DirMove)

CheckForIllegalCrossThreadCalls = False

SourceDir = TextBox1.Text
DestDir = TextBox2.Text

SourceLen = GetFolderSize(SourceDir)

ProgressBar1.Value = 0
Timer1.Interval = 100
Timer1.Start()
T.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Directory.Exists(DestDir) = False Then Exit Sub
If SourceLen <> 0 Then
ProgressBar1.Value = Math.Round(GetFolderSize(DestDir) / SourceLen, 2) * 100
End If

End Sub

Private Sub DirMove()
Me.Text = "正在移动..."
Directory.Move(SourceDir, DestDir)
Me.Text = "移动完成!"
ProgressBar1.Value = 100
RichTextBox1.Text += DestDir & Environment.NewLine
End Sub

Private Sub DirCopy()
My.Computer.FileSystem.CopyDirectory(SourceDir, DestDir, True, FileIO.UICancelOption.DoNothing)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim T As New Thread(AddressOf DirCopy)

CheckForIllegalCrossThreadCalls = False

SourceDir = TextBox1.Text
DestDir = TextBox2.Text

SourceLen = GetFolderSize(SourceDir)

ProgressBar1.Value = 0
Timer1.Interval = 100
Timer1.Start()
T.Start()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim a As New ArrayList
a.Add(".MP3")
Dim b As New ArrayList
Dim c As New Hashtable

clsUtil.findDirectory("D:/a", a, b, c)
End Sub
End Class

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: