PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms 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. #region Using directives
  18. using System;
  19. using System.IO;
  20. using System.Collections.Generic;
  21. using System.ComponentModel;
  22. using System.Data;
  23. using System.Drawing;
  24. using System.Linq;
  25. using System.Text;
  26. using System.Windows.Forms;
  27. #endregion
  28. namespace CSListFilesInDirectory
  29. {
  30. public partial class MainForm : Form
  31. {
  32. public MainForm()
  33. {
  34. InitializeComponent();
  35. }
  36. private void btnList_Click(object sender, EventArgs e)
  37. {
  38. try
  39. {
  40. int i = 0;
  41. DirectoryEnumerator.Mode mode =
  42. (chkDir.Checked ? DirectoryEnumerator.Mode.Directory : 0) |
  43. (chkFiles.Checked ? DirectoryEnumerator.Mode.File : 0);
  44. lstFiles.Items.Clear();
  45. string pattern = Path.Combine(txtDir.Text.Trim(), txtPattern.Text.Trim());
  46. DirectoryEnumerator de = new DirectoryEnumerator(pattern, mode);
  47. foreach (string file in de) // Enumerate items in the directory
  48. {
  49. i++; // Increase the count
  50. lstFiles.Items.Add(file);
  51. Application.DoEvents();
  52. }
  53. lblCount.Text = string.Format("{0:#,##0} Item{1}", i, i != 1 ? "s" : string.Empty);
  54. }
  55. catch (Win32Exception winEx)
  56. {
  57. MessageBox.Show(winEx.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  58. }
  59. }
  60. private void txtDir_TextChanged(object sender, EventArgs e)
  61. {
  62. // Enable the List button if the target directory exists
  63. btnList.Enabled = Directory.Exists(txtDir.Text.Trim());
  64. }
  65. private void chkModes_CheckedChanged(object sender, EventArgs e)
  66. {
  67. // Enable the List button if at least one check box is selected
  68. btnList.Enabled = chkDir.Checked || chkFiles.Checked;
  69. }
  70. }
  71. }