VB ConsoleProgressBar简单介绍

在向大家详细介绍VB ConsoleProgressBar之前,首先让大家了解下CopyFiles,然后全面介绍VB ConsoleProgressBar。CopyFiles负责取得源目录下的一个文件列表,然后把它们复制到目的目录下。另外,它还创建了一个ConsoleProgressBar对象来管理进度条:

 
 
 
  1. Private Sub CopyFiles(ByVal srcDir As String, ByVal destDir As String)  
  2. Const BufferSourceTopLine As Integer = 8 
  3. Const BufferDestinationTopLine As Integer = 7 
  4. Dim rowIndex As Integer = 7 
  5. Dim originalForegroundColor As ConsoleConsoleColor = Console.ForegroundColor  
  6. Console.CursorVisible = False 
  7. Console.Clear()  
  8. Dim numberOfFiles As Integer  
  9. numberOfFiles = My.Computer.FileSystem.GetFiles(srcDir).Count  
  10. Dim PB As New ConsoleProgressBar(numberOfFiles)  
  11. DisplayHeader(srcDir, destDir)  
  12. Dim fileCounter As Integer = 1 
  13. For Each f As String In My.Computer.FileSystem.GetFiles(srcDir)  
  14. Dim fi As New System.IO.FileInfo(f)  
  15. Console.ForegroundColor = ConsoleColor.Green  
  16. Console.SetCursorPosition(0, rowIndex)  
  17. Console.Write(fi.Name)  
  18. If rowIndex < Console.WindowHeight - 1 Then  
  19. rowIndex += 1  
  20. Else  
  21. Console.MoveBufferArea(0,BufferSourceTopLine, _  
  22. Console.WindowWidth, _  
  23. Console.WindowHeight - _  
  24. BufferSourceTopLine, _  
  25. 0, _  
  26. BufferDestinationTopLine)  
  27. End If  
  28. My.Computer.FileSystem.CopyFile(fi.FullName, destDir &"\" & fi.Name)  
  29. pb.Update( fileCounter)  
  30. fileCounter += 1  
  31. Next  
  32. Console.ForegroundColor = originalForegroundColor 
  33. Console.SetCursorPosition(0, Console.WindowHeight - 1)  
  34. Console.CursorVisible = True 
  35. End Sub  

首先,该代码保存当前的ForegroundColor。然后,它使用另外一种新特征把CursorVisible属性设置为False。在清除控制台窗口后,它检索在源目录下的文件个数并且使用这个数字作为VB ConsoleProgressBar构造器的***值。稍后,我还要讨论有关于 ConsoleProgressBar的细节信息。

我调用DisplayHeader子程序来把一些有关复制操作的信息打印到控制台窗口。由于其功能非常类似于DisplayUsage子程序,所以我在此省略对其细节的讨论。

我使用了一个“For...Each”循环来遍历在源目录下的所有文件,并使用了一个rowIndex变量来跟踪要把文件名打印到控制台的哪一行。随着循环的不断执行,rowIndex每次增加1,直到到达控制台窗口的底部为止。一旦到达控制台窗口的底部,我利用另外一个新控制台应用程序特征— MoveBufferArea方法(下一节讨论)。

在更新显示和复制文件后,我通过调用VB ConsoleProgressBar类的Update方法来更新进度条。

一旦循环结束并且文件复制结束,我再把ForegroundColor设置回其原来的颜色,并且把光标位置设置到控制台窗口的最下面一行,并且使之再次可见。

【编辑推荐】

  1. VB 2005中开发新一代控制台应用程序
  2. 简单讲解VB开发分布式
  3. 浅析VB Script开发自动化测试
  4. 浅谈VB开发系统知识
  5. VB.NET应用程序中多线程的应用实例
THE END