VB ConsoleProgressBar簡單介紹
在向大家詳細介紹VB ConsoleProgressBar之前,首先讓大家了解下CopyFiles,然后全面介紹VB ConsoleProgressBar。CopyFiles負責取得源目錄下的一個文件列表,然后把它們復(fù)制到目的目錄下。另外,它還創(chuàng)建了一個ConsoleProgressBar對象來管理進度條:
- Private Sub CopyFiles(ByVal srcDir As String, ByVal destDir As String)
- Const BufferSourceTopLine As Integer = 8
- Const BufferDestinationTopLine As Integer = 7
- Dim rowIndex As Integer = 7
- Dim originalForegroundColor As ConsoleConsoleColor = Console.ForegroundColor
- Console.CursorVisible = False
- Console.Clear()
- Dim numberOfFiles As Integer
- numberOfFiles = My.Computer.FileSystem.GetFiles(srcDir).Count
- Dim PB As New ConsoleProgressBar(numberOfFiles)
- DisplayHeader(srcDir, destDir)
- Dim fileCounter As Integer = 1
- For Each f As String In My.Computer.FileSystem.GetFiles(srcDir)
- Dim fi As New System.IO.FileInfo(f)
- Console.ForegroundColor = ConsoleColor.Green
- Console.SetCursorPosition(0, rowIndex)
- Console.Write(fi.Name)
- If rowIndex < Console.WindowHeight - 1 Then
- rowIndex += 1
- Else
- Console.MoveBufferArea(0,BufferSourceTopLine, _
- Console.WindowWidth, _
- Console.WindowHeight - _
- BufferSourceTopLine, _
- 0, _
- BufferDestinationTopLine)
- End If
- My.Computer.FileSystem.CopyFile(fi.FullName, destDir &"\" & fi.Name)
- pb.Update( fileCounter)
- fileCounter += 1
- Next
- Console.ForegroundColor = originalForegroundColor
- Console.SetCursorPosition(0, Console.WindowHeight - 1)
- Console.CursorVisible = True
- End Sub
首先,該代碼保存當前的ForegroundColor。然后,它使用另外一種新特征把CursorVisible屬性設(shè)置為False。在清除控制臺窗口后,它檢索在源目錄下的文件個數(shù)并且使用這個數(shù)字作為VB ConsoleProgressBar構(gòu)造器的***值。稍后,我還要討論有關(guān)于 ConsoleProgressBar的細節(jié)信息。
我調(diào)用DisplayHeader子程序來把一些有關(guān)復(fù)制操作的信息打印到控制臺窗口。由于其功能非常類似于DisplayUsage子程序,所以我在此省略對其細節(jié)的討論。
我使用了一個“For...Each”循環(huán)來遍歷在源目錄下的所有文件,并使用了一個rowIndex變量來跟蹤要把文件名打印到控制臺的哪一行。隨著循環(huán)的不斷執(zhí)行,rowIndex每次增加1,直到到達控制臺窗口的底部為止。一旦到達控制臺窗口的底部,我利用另外一個新控制臺應(yīng)用程序特征— MoveBufferArea方法(下一節(jié)討論)。
在更新顯示和復(fù)制文件后,我通過調(diào)用VB ConsoleProgressBar類的Update方法來更新進度條。
一旦循環(huán)結(jié)束并且文件復(fù)制結(jié)束,我再把ForegroundColor設(shè)置回其原來的顏色,并且把光標位置設(shè)置到控制臺窗口的最下面一行,并且使之再次可見。
【編輯推薦】