PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/VBListFilesInDirectory/MainForm.vb

#
Visual Basic | 55 lines | 27 code | 10 blank | 18 comment | 0 complexity | 7a6871e63ddda425fca0c030496c583c MD5 | raw file
  1. '/************************************* Module Header **************************************\
  2. '* Module Name: MainForm.vb
  3. '* Project: VBListFilesInDirectory
  4. '* Copyright (c) Microsoft Corporation.
  5. '*
  6. '* The VBListFilesInDirectory project demonstrates how to implement an IEnumerable(Of String)
  7. '* that utilizes the Win32 File Management functions to enable application to get files and
  8. '* sub-directories in a specified directory one item a time.
  9. '*
  10. '* This source is subject to the Microsoft Public License.
  11. '* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  12. '* All other rights reserved.
  13. '*
  14. '* History:
  15. '* * 8/31/2009 10:00 AM Jie Wang Created
  16. '\******************************************************************************************/
  17. Public Class MainForm
  18. Private Sub btnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
  19. Try
  20. Dim i As Integer = 0
  21. Dim mode As DirectoryEnumerator.Mode
  22. If chkDir.Checked Then mode = mode Or DirectoryEnumerator.Mode.Directory
  23. If chkFiles.Checked Then mode = mode Or DirectoryEnumerator.Mode.File
  24. lstFiles.Items.Clear()
  25. Dim pattern As String = Path.Combine(txtDir.Text.Trim(), txtPattern.Text.Trim())
  26. Dim de As New DirectoryEnumerator(pattern, mode)
  27. For Each file As String In de ' Enumerate items in the directory
  28. i += 1 ' Increase the count
  29. lstFiles.Items.Add(file)
  30. Application.DoEvents()
  31. Next
  32. lblCount.Text = String.Format("{0:#,##0} Item{1}", i, IIf(i <> 1, "s", String.Empty))
  33. Catch winEx As Win32Exception
  34. MsgBox(winEx.Message, vbOKOnly Or vbCritical, Me.Text)
  35. End Try
  36. End Sub
  37. Private Sub txtDir_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDir.TextChanged
  38. ' Enable the List button if the target directory exists
  39. btnList.Enabled = Directory.Exists(txtDir.Text.Trim())
  40. End Sub
  41. Private Sub chkModes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDir.CheckedChanged, chkFiles.CheckedChanged
  42. ' Enable the List button if at least one check box is selected
  43. btnList.Enabled = chkDir.Checked OrElse chkFiles.Checked
  44. End Sub
  45. End Class