frmexample0.vb

来自「Microsoft Mobile Development Handbook的代码」· VB 代码 · 共 77 行

VB
77
字号
Imports Microsoft.VisualBasic
Imports System
Imports System.Windows.Forms
Imports System.Diagnostics
Imports System.Collections
Imports System.IO
Imports System.Threading

Namespace CodeForChapter11cs
  Public Partial Class frmExample0
	  Inherits Form
	Public Sub New()
	  InitializeComponent()
	End Sub

	Private allFiles As ArrayList = New ArrayList() ' holds the results
	Private stopRequested As Boolean = False ' user hit the cancel button

	Private Sub menuItem2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles menuItem2.Click
	  ' cancel
	  stopRequested = True
	End Sub

	Private Sub menuItem1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles menuItem1.Click
	  ' load list
	  label1.Text = ""
	  listBox1.DataSource = Nothing
	  stopRequested = False

	  Me.GetAllFiles() 'will have populated the allFiles ArrayList

	  label1.Text = allFiles.Count.ToString()
	  listBox1.DataSource = allFiles
	  Debug.WriteLine("Got results " & listBox1.Items.Count.ToString())
	End Sub

	Private Sub GetAllFiles()
	  allFiles.Clear()
	  Me.PopulateAllFilesFor("\")
	  Debug.WriteLine("Finished")
	End Sub

	Private Sub PopulateAllFilesFor(ByVal path As String)
      ' "non-ideal solutions"
      ' Me.Refresh()          //Non-ideal solution 1
      ' Application.DoEvents()  //Non-ideal solution 2      

	  If stopRequested Then
		Debug.WriteLine("cancelled 1")
		Return
	  End If
	  Debug.WriteLine("Processing new path")

	  allFiles.Add(path)

	  Dim files As String()
	  files = Directory.GetFiles(path)
	  allFiles.AddRange(files)

	  Debug.WriteLine(allFiles.Count.ToString())
	  label1.Text = allFiles.Count.ToString()

	  If stopRequested Then
		Debug.WriteLine("cancelled 2")
		Return
	  End If

	  For Each subDirectory As String In Directory.GetDirectories(path)
		Me.PopulateAllFilesFor(subDirectory) 'recursion
		If stopRequested Then
		  Debug.WriteLine("cancelled 3")
		  Return
		End If
	  Next subDirectory
	End Sub
  End Class
End Namespace

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?