Advertisement
stanevplamen

02.2a.11.2.DisplaySearchBFSAlgorithm

Jun 16th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.90 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.IO;
  7.  
  8. class TraverseDir
  9. {
  10.     static void Main(string[] args)
  11.     {
  12.         string[] interestingDirNames = {
  13.             "microsoft",
  14.             "dx",
  15.             "directx",
  16.             "sdk",
  17.             "utilities",
  18.             "bin",
  19.             "x86" };
  20.  
  21.         List<string> startDirs = new List<string>();
  22.  
  23.         startDirs.Add(@"c:\ATI");
  24.         //startDirs.Add(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
  25.  
  26.         //foreach (System.IO.DriveInfo Drive in System.IO.DriveInfo.GetDrives())
  27.         //    if (Drive.DriveType == System.IO.DriveType.Fixed)
  28.         //        startDirs.Add(Drive.RootDirectory.Name);
  29.  
  30.         string result = IntelligentSearch(
  31.             "plm_supertap.txt",
  32.             false,
  33.             interestingDirNames,
  34.             startDirs.ToArray(),
  35.             TimeSpan.FromMilliseconds(50000.0));
  36.     }
  37.  
  38.     public static string IntelligentSearch(
  39.         string targetName, bool targetIsDirectory,
  40.         string[] interestingDirNames, string[] startDirs, TimeSpan timeout)
  41.     {
  42.         DateTime endTime = DateTime.Now + timeout;
  43.  
  44.         // Paths to process
  45.         LinkedList<string> PathQueue = new LinkedList<string>();
  46.         // Path already processed in form Key=Path, Value="1"
  47.         System.Collections.Specialized.StringDictionary Processed =
  48.             new System.Collections.Specialized.StringDictionary();
  49.  
  50.         foreach (string startDir in startDirs)
  51.             PathQueue.AddLast(startDir);
  52.  
  53.         // Processing loop - berform breadth first search (BFS) algorithm
  54.         while (PathQueue.Count > 0)
  55.         {
  56.             // Get directory to process
  57.             string Dir = PathQueue.First.Value;
  58.             //  get and write filenames
  59.             DirectoryInfo directory = new DirectoryInfo(Dir);
  60.             FileInfo[] files = directory.GetFiles();
  61.             for (int index = 0; index < files.Length; index++)
  62.             {
  63.                 Console.WriteLine(files[index].FullName);
  64.             }
  65.            
  66.             //
  67.             Console.WriteLine(Dir);
  68.             PathQueue.RemoveFirst();
  69.             // Already processed
  70.             if (Processed.ContainsKey(Dir))
  71.                 continue;
  72.             // Add to processed
  73.             Processed.Add(Dir, "1");
  74.  
  75.             try
  76.             {
  77.                 string targetPath = Path.Combine(Dir, targetName);
  78.                 if (targetIsDirectory)
  79.                 {
  80.                     if (Directory.Exists(targetPath))
  81.                         return targetPath;
  82.                 }
  83.                 else
  84.                 {
  85.                     if (File.Exists(targetPath))
  86.                         return targetPath;
  87.                 }
  88.  
  89.                 foreach (string SubDir in Directory.GetDirectories(Dir))
  90.                 {
  91.                     bool dirIsInteresting = false;
  92.                     if (interestingDirNames != null && interestingDirNames.Length > 0)
  93.                     {
  94.                         string subDirName = Path.GetFileName(SubDir);
  95.                         foreach (string interestingName in interestingDirNames)
  96.                         {
  97.                             if (subDirName.IndexOf(interestingName, StringComparison.InvariantCultureIgnoreCase) != -1)
  98.                             {
  99.                                 dirIsInteresting = true;
  100.                                 break;
  101.                             }
  102.                         }
  103.                     }
  104.  
  105.                     if (dirIsInteresting)
  106.                         PathQueue.AddFirst(SubDir);
  107.                     else
  108.                         PathQueue.AddLast(SubDir);
  109.                 }
  110.             }
  111.             catch (Exception) { }
  112.  
  113.             if (DateTime.Now > endTime)
  114.                 return null;
  115.         }
  116.  
  117.         return null;
  118.     }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement