Advertisement
tankcr

Untitled

May 22nd, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.23 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.IO;
  8.  
  9. namespace MusicLibUtility
  10. {
  11.     public class RecursiveFileSearch
  12.     {
  13.         static System.Collections.Specialized.StringCollection log = new System.Collections.Specialized.StringCollection();
  14.  
  15.         public void WalkDirectoryTree(System.IO.DirectoryInfo rootDir)
  16.         {
  17.             System.IO.FileInfo[] files = null;
  18.             System.IO.DirectoryInfo[] subDirs = null;
  19.  
  20.             // First, process all the files directly under this folder
  21.             try
  22.             {
  23.                 files = rootDir.GetFiles("*.*");
  24.             }
  25.             // This is thrown if even one of the files requires permissions greater
  26.             // than the application provides.
  27.             catch (UnauthorizedAccessException e)
  28.             {
  29.                 // This code just writes out the message and continues to recurse.
  30.                 // You may decide to do something different here. For example, you
  31.                 // can try to elevate your privileges and access the file again.
  32.                 log.Add(e.Message);
  33.             }
  34.  
  35.             catch (System.IO.DirectoryNotFoundException e)
  36.             {
  37.                 Console.WriteLine(e.Message);
  38.             }
  39.  
  40.             if (files != null)
  41.             {
  42.                 foreach (System.IO.FileInfo fi in files)
  43.                 {
  44.                     // In this example, we only access the existing FileInfo object. If we
  45.                     // want to open, delete or modify the file, then
  46.                     // a try-catch block is required here to handle the case
  47.                     // where the file has been deleted since the call to TraverseTree().
  48.                     Console.WriteLine(fi.FullName);
  49.                 }
  50.  
  51.                 // Now find all the subdirectories under this directory.
  52.                 subDirs = rootDir.GetDirectories();
  53.  
  54.                 foreach (System.IO.DirectoryInfo dirInfo in subDirs)
  55.                 {
  56.                     // Resursive call for each subdirectory.
  57.                     WalkDirectoryTree(dirInfo);
  58.                 }
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement