views:

223

answers:

3

How can I have a webservice read/browse a folder content ?

For instance this type of code:

    FolderBrowserDialog folderBrowser;
    folderBrowser = new System.Windows.Forms.FolderBrowserDialog();

    folderBrowser.Description = "...";
    folderBrowser.ShowNewFolderButton = false;
    folderBrowser.RootFolder = Environment.SpecialFolder.MyComputer;

When I build the solution I get this error...

The type or namespace name 'FolderBrowserDialog' could not be found (are you missing a using directive or an assembly reference?)

I know it doesn't make a lot of sense trying to use a dialog in a webservice but how else can I do it?

My webservice receives a string and then I want to browse for files that contain that string in a folder.

+1  A: 

You'll need to use System.IO namespace to navigate into your filesystem; as you noted, doesn't make sense trying to display a dialog on a webservice call.

Rubens Farias
+1  A: 

Have a look at the System.IO.Directory.GetFiles() method. Displaying the FolderBrowser dialog can naturally only be used with thick client interactive WinForms apps.

Wim Hollebrandse
+1  A: 

Use a StreamReader to read a text file:

StreamReader reader = File.OpenText(filename);

string contents = reader.ReadToEnd();

reader.Close();

To list files in a folder:

 DirectoryInfo di = new DirectoryInfo(fullPathToFolder);
 FileInfo[] fileList = di.GetFiles("*.aspx");

 foreach(FileInfo fi in fileList)
 {
     // do something with fi.Name
 }
Ariel