PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSListFilesInDirectory/MainForm.cs

#
C# | 81 lines | 53 code | 10 blank | 18 comment | 2 complexity | 33d91f6493c5aa3c353b27d0841af19f MD5 | raw file
 1/************************************* Module Header **************************************\
 2* Module Name:  MainForm.cs
 3* Project:      CSListFilesInDirectory
 4* Copyright (c) Microsoft Corporation.
 5* 
 6* The CSListFilesInDirectory project demonstrates how to implement an IEnumerable<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* * 7/7/2009 8:00 PM Jie Wang Created
16\******************************************************************************************/
17
18#region Using directives
19using System;
20using System.IO;
21using System.Collections.Generic;
22using System.ComponentModel;
23using System.Data;
24using System.Drawing;
25using System.Linq;
26using System.Text;
27using System.Windows.Forms;
28#endregion
29
30
31namespace CSListFilesInDirectory
32{
33    public partial class MainForm : Form
34    {
35        public MainForm()
36        {
37            InitializeComponent();
38        }
39
40        private void btnList_Click(object sender, EventArgs e)
41        {
42            try
43            {
44                int i = 0;
45                DirectoryEnumerator.Mode mode =
46                    (chkDir.Checked ? DirectoryEnumerator.Mode.Directory : 0) |
47                    (chkFiles.Checked ? DirectoryEnumerator.Mode.File : 0);
48
49                lstFiles.Items.Clear();
50
51                string pattern = Path.Combine(txtDir.Text.Trim(), txtPattern.Text.Trim());
52                DirectoryEnumerator de = new DirectoryEnumerator(pattern, mode);
53                
54                foreach (string file in de) // Enumerate items in the directory
55                {
56                    i++; // Increase the count
57                    lstFiles.Items.Add(file);
58                    Application.DoEvents();
59                }
60
61                lblCount.Text = string.Format("{0:#,##0} Item{1}", i, i != 1 ? "s" : string.Empty);
62            }
63            catch (Win32Exception winEx)
64            {
65                MessageBox.Show(winEx.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
66            }
67        }
68
69        private void txtDir_TextChanged(object sender, EventArgs e)
70        {
71            // Enable the List button if the target directory exists
72            btnList.Enabled = Directory.Exists(txtDir.Text.Trim());
73        }
74
75        private void chkModes_CheckedChanged(object sender, EventArgs e)
76        {
77            // Enable the List button if at least one check box is selected
78            btnList.Enabled = chkDir.Checked || chkFiles.Checked;
79        }
80    }
81}