Joao Heleno
Joao Heleno

Reputation: 370

C# accessing folder/files thru webservice

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.

Upvotes: 0

Views: 2395

Answers (3)

Ariel
Ariel

Reputation: 4500

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
 }

Upvotes: 1

Wim
Wim

Reputation: 12082

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.

Upvotes: 1

Rubens Farias
Rubens Farias

Reputation: 57936

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.

Upvotes: 1

Related Questions