2

What would be the fastest way to search for a file programtically in C#. I know the relative location of the file, lets say its "abcd\efgh\test.txt". I also know that this file is on my E:\ drive. "abcd" is a subdirectory on some directory in E:\ drive.

Thanks

dsilva
  • 55
  • 3
  • A nit: C# has no file I/O methods. You'll be searching for the file using .NET, and only writing your code in C#. As such, it would be good for you to tell us which .NET Framework version you'll be using, as well as which C# version you'll be using. For instance, you might be using .NET 2.0 with C# 3.0. – John Saunders Jun 23 '10 at 15:01
  • Yup I m using .Net 2.0 with C# 3.0 – dsilva Jun 23 '10 at 15:11

5 Answers5

2

Since you know the root directory you want to search and a string pattern for a filename, you can create a DirectoryInfo with the root directory:

DirectoryInfo dir = new DirectoryInfo(@"E:\");

And then call GetFiles() to get all the matches. Passing SearchOption.AllDirectories will ensure the search is recursive.

List<FileInfo> matches = 
    new List<FileInfo>(dir.GetFiles(partialFilename, 
        SearchOption.AllDirectories));

Or if you know part of the path (instead of the filename):

List<DirectoryInfo> matches =
    new List<DirectoryInfo>(dir.GetDirectories(partialDirectoryName,
        SearchOption.AllDirectories));

And then you can navigate to the file from there.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
0

So if I understand correctly you know the path will be of the form:

"E:\\" + something + "\\abcd\\efgh\\test.txt"

If something is only 1 level deep, then simply get all directories on your E:, and then try to do a file open on each of those subdirectories.

If something is more than 1 level deep, then do a recursive call to get the directories until you find abcd.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
0

The fastest way would probably be to use FindFirstFile etc api but I would have thought that you could do it fairly quickly (And much easier) with Directory.GetDirectories (to find the right sub direcotry) and then Directory.GetFiles to find the actual file.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
0

Just the use File.Exists method and iterate through all possible filenames. Maybe something like this (not tested):

 string SearchInFolder(string root) {

    if (File.Exists(Path.Append(root, "\\abcd\\efgh\\test.txt")) return Path.Append(root, "\\abcd\\efgh\\test.txt");
    foreach(var folder in Directory.GetDirectories(root)) {
       var fullFile = Path.Append(folder, "\\abcd\\efgh\\test.txt");
       if (File.Exists(fullFile)) {
           // Found it !!!!
           return fullFile;
       } else {
           var result = SearchInFolder(folder);
           if (result != null) return result;
       }
    }
    return null;
 }

But this will seach the whole E:\ drive for the pattern, also subfolders are searched.

GvS
  • 52,015
  • 16
  • 101
  • 139
0

I think the algorithm would be start at e:\ and read all directories. If one of them is \abcd\, immediately check for the rest of the path and file, e.g., efgh\test.txt.

If e:\ does NOT have \abcd\, then you need to traverse into each subdirectory and do the same check. Does \abcd\ exist? Yes, check for efgh\text.txt. No? Traverse subdirs.

If you need the absolutely fastest, when you fail to find \abcd\, and you have your list of subdirs you must now go check, you could introduce some level of threading. But that could get hairy rather quickly.

Brett McCann
  • 2,469
  • 2
  • 27
  • 44