tags:

views:

307

answers:

5

I would like to write a web page where users can search a directory on NTFS for certain criteria and display the results.

Does the directory have to be indexed?

Is there a .NET accessible windows search API that I can use to query the directory?

+3  A: 

All you get is System.IO.DirectoryInfo, and it doesn't even have built in searching.

Most people end up writing a recursive method that calls getDirectories and getFiles on each DirectoryInfo.

FlySwat
Yup, we sure do...*sigh*
Ed Swangren
+1  A: 

You can use the windows search API this way, but I don't really like that way.

If you have time to index by yourself the directory, I recommend you to try Lucene.net, which is faster than the windows indexing service for sure.

Anyways, the shortest way is to use System.IO.DirectoryInfo.GetDirectories.

Diego Jancic
+1  A: 

DirectoryInfo.GetFiles does support recursing through subdirectories. It's one of the overloaded methods. It allows you to filter by filename only though.

Matthew Steeples
+1  A: 

If you are using .NET 2.0 or greater (and VB.Net) then you can use the My.Computer.FileSystem.GetFiles Method. This method has three parameters:

  • Directory
  • SearchType (enumeration)
  • Wildcards

The SearchType enumeration has two values one of which is SearchAllSubDirectories. So 1 method which returns a ReadOnly String Collection of all the file (names) that match.

If you are using C# then you could use the DirectoryInfo.GetFiles Method . There are only 2 parameters with DirectoryInfo.GetFiles:

  • SearchPattern
  • SearchOption

If you specify AllDirectories for the SearchOption you'd get a similar result.

The key difference being that the FileSystem.GetFiles returns as Collection of Strings while the DirectoryInfo.GetFiles returns an array of FileInfo Objects.

CertifiedCrazy
+2  A: 

This is a reusable class called FileSelector that does file selection based on name (with wildcards), size, timestamp, and attributes.

Usage is like this:

   Ionic.FileSelector ff = new Ionic.FileSelector(selectionCriteria);
   var filesToAdd = ff.SelectFiles(directoryOnDisk, recurseDirectories);

The selectionCriteria are like this:

*.txt
name = *.txt
size > 10000
name = *.txt and size > 20000
attributes = H and name = *.doc
name = *.cs~ and mtime < 2009-02-14
mtime < 2009-01-01 and (name = *.cs or name = *.vb)
(name = '* Report.docx'  or  name = *.pptx)

...where mtime implies "last write time", atime implies "last access time", ctime implies "creation time", and attributes refers to the NTFS attribute set, like System, Hidden, Readonly, Archive, and ContentIndexed (S, H, R, A, I).

The single quotes are necessary around the filename patterns only when there is an intervening space.

You can grab the source for this file selector class and use it anywhere.

Cheeso