2

Assume that a website has set the directory listing permissions open for one of its folder and I can see files on the web browser[as on the image]. Is there a way to get files in this folder to an array?

enter image description here

In a word, I need to use a method that's doing what DirectoryInfo.GetFiles("C:/"); for an URL.

Edit: I'd rather want to use a method instead of getting web response and parse the result. That's like a back door in case there's not an alternative way.

kubilay
  • 5,047
  • 7
  • 48
  • 66
  • just one folder or are you looking to get all files in sub directory as well? You can use webclient to load the html and use regex to get that information – Esen Aug 09 '12 at 19:54
  • @Esen, I can do it again for sub directories, that's all right. But I'd be glad if you could explain your solution or point a sample etc. – kubilay Aug 09 '12 at 19:58

1 Answers1

4

This code will help you get the html

             WebRequest request = WebRequest.Create("http://yourwebsite/yourwebdirectory/");
             var webResponse=request.GetResponse();
             Stream dataStream = webResponse.GetResponseStream(); 
             StreamReader reader = new StreamReader(dataStream); 
             string responseFromServer = reader.ReadToEnd(); 
             Console.WriteLine(responseFromServer); 
             reader.Close();
             webResponse.Close();

Once you get the html you can parse it to get the file names

Esen
  • 973
  • 1
  • 21
  • 47
  • By the way u need to add "using system.net;" – Esen Aug 09 '12 at 20:08
  • thank you for the idea. I actually considered this, my bad not to claim in the question. Let's hope for an equivalent to DirectoryInfo.GetFiles() method. If not, then yours is the only solution. – kubilay Aug 09 '12 at 20:10
  • 2
    By the way I just come across and found http://stackoverflow.com/a/124522/1221319 discuss the same. – Esen Aug 09 '12 at 20:14