1

I have win service which must download all zip files by URL(for example http://download.geonames.org/export/dump/) but when i use Directory.GetFiles method or DirectoryInfo di = new DirectoryInfo(ConfigurationManager.AppSettings["GeoFullDataURLPath"]) i get error:

URI formats are not supported..

How i can resolve this problem?

M4N
  • 94,805
  • 45
  • 217
  • 260
Alexey Z.
  • 189
  • 1
  • 3
  • 14

3 Answers3

2

You have to make a WebRequest. DirectoryInfo is for local drives and SMB shares only I believe. This should do the trick : http://www.csharp-examples.net/download-files/

Myles McDonnell
  • 12,943
  • 17
  • 66
  • 116
  • http://www.csharp-examples.net/download-files/ in this link - how to download files, but i need list of all files by url, not how download.. – Alexey Z. Dec 12 '11 at 11:27
  • You will have to parse the HTML of the page that lists the files in order to ascertain the URL of each target. Shai's answer has a link to something that might help. – Myles McDonnell Dec 12 '11 at 11:44
2

You can't use Directory.GetFiles on a URL.

Consider the following example:

 WebClient webClient = new WebClient();
 webClient.DownloadFile("http://download.geonames.org/export/dump/file.zip", "new-file.zip");

This will download the file file.zip from the URL above.

Directory listings over web is usually blocked for security reasons,

EDIT: See this

Community
  • 1
  • 1
Shai
  • 25,159
  • 9
  • 44
  • 67
  • 1
    @AlexeyZ. AFAIK there is no built in function with which you can do that,I guess its time to get your hands dirty (with the code of-course) :) – Vamsi Dec 12 '11 at 11:26
1

The full source code for performing this task is:

string
    storeLocation = "C:\\dump",
    fileName = "",
    baseURL = "http://download.geonames.org/export/dump/";

WebClient r = new WebClient();            
string content = r.DownloadString(baseURL);
foreach (Match m in Regex.Matches(content, "<a href=\\\"[^\\.]+\\.zip\">"))
{
    fileName = Regex.Match(m.Value, "\\w+\\.zip").Value;
    r.DownloadFile(baseURL + fileName, Path.Combine(storeLocation, fileName));
}