PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 7ms 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
18Public Class MainForm
19
20    Private Sub btnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
21        Try
22            Dim i As Integer = 0
23            Dim mode As DirectoryEnumerator.Mode
24
25            If chkDir.Checked Then mode = mode Or DirectoryEnumerator.Mode.Directory
26            If chkFiles.Checked Then mode = mode Or DirectoryEnumerator.Mode.File
27
28            lstFiles.Items.Clear()
29
30            Dim pattern As String = Path.Combine(txtDir.Text.Trim(), txtPattern.Text.Trim())
31            Dim de As New DirectoryEnumerator(pattern, mode)
32
33            For Each file As String In de   ' Enumerate items in the directory
34                i += 1  ' Increase the count
35                lstFiles.Items.Add(file)
36                Application.DoEvents()
37            Next
38
39            lblCount.Text = String.Format("{0:#,##0} Item{1}", i, IIf(i <> 1, "s", String.Empty))
40
41        Catch winEx As Win32Exception
42            MsgBox(winEx.Message, vbOKOnly Or vbCritical, Me.Text)
43        End Try
44    End Sub
45
46    Private Sub txtDir_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDir.TextChanged
47        ' Enable the List button if the target directory exists
48        btnList.Enabled = Directory.Exists(txtDir.Text.Trim())
49    End Sub
50
51    Private Sub chkModes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDir.CheckedChanged, chkFiles.CheckedChanged
52        ' Enable the List button if at least one check box is selected
53        btnList.Enabled = chkDir.Checked OrElse chkFiles.Checked
54    End Sub
55End Class