Reputation: 45
Is there a way to find a file's directory without knowing the directory? For example, I have calc.exe somewhere on my computer. Is there a way to find that it's installed on C:\windows\system32?
Ideally, i'd like to be able to find other executables that are installed elsewhere, I'm just curious how to search for a file name and extension, returning the full path.
I am hoping to do this in .NET by the way.
Upvotes: 1
Views: 269
Reputation: 14133
Simply searching all directories is fairly easy. The problem is that hard disks (and to a lesser extent, solid state disks) are still the slowest component in modern computers.
The best place to start is offering to let the user specify a directory that should be searched. It's as effective as it is simple. There is a huge difference between scanning all of C:\
, or just the contents of C:\Windows
.
Then, you'll have to decide how you want to scan sub-directories. Do you want to search horizontally or vertically? In other words: when you hit a sub-directory in your search, should you immediately start scanning the contents of that directory, or should you finish scanning the current directory first? If the current directory has both a sibling and a sub-directory, which one do you want to scan first?
Finally, you'll have to decide how many levels deep you should scan. If the file hasn't been found in the 10th sub-directory, is it still worth looking through the 11th or 12th?
For actual code examples: click
Upvotes: 2