views:

214

answers:

1

Hello

I need a file search for my Visual Basic 2008 program. Do you guys know a way to start off? I've heard that you need admin priveledge for some file searches, but I don't want any of those kind of things since it would be a bug to the user.

Thanks,

KEvin

A: 

The System.IO.Directory Class should help you here.

Also you may want to build a catalog system or find a system that supports an API. This way you dont have to walk a directory structure every time the user performs a query.

Basically you can do (pseudo code, adjust to fit):

Dim fn as File

For Each fn in System.IO.Directory.GetFiles(SomePath,"*.*",System.IO.SearchOption.AllDirectories)
  ' Do something with the fn
End For

Now there is no way around the NTFS ACL's and you would need to handle this as appropriate.

Have a look at this post to hook into Windows Desktop Search from C#.

Microsoft Windows Search 3.x SDK

Brief Description

Version 1.0

Download the Windows Search Software Development Kit to explore sample applications and develop using Visual Studio, C#, and .NET technology.

Update 1

Function that does things with files in directories (pseudo code, adjust to fit):

Public Function MyDirectoryFunction(ByVal dir as String, ByVal fileMatch as String,  ByVal beRecursive as Boolean)
    Dim fn as File
    Dim searchOpt as System.IO.SearchOption

    If beRecursive = True Then
      searchOpt = System.IO.SearchOption.AllDirectories
    Else
      searchOpt = System.IO.SearchOption.TopDirectoryOnly
    End If

    If String.IsNullOrEmpty(fileMatch) Then
      fileMatch = "*.*"
    End If

    For Each fn in System.IO.Directory.GetFiles(dir, fileMatch, searchOpt)
      ' Do something with the fn
    End For
End Function
Wayne
Ok I got the file search to work out the way I want it to, but when I try searching the whole C:\ for the file, it would give me the error that it can't find c:\documents and settings. I'm on vista so its a bit different. How would I be able to get the program to search only directories that exist?
Kevin
Wrap the above code into a function that takes a parameter for the directory to search is one way of achieving what you require
Wayne