2011-12-12 55 views
1

我有贏的服務,必須由URL下載所有zip文件(例如http://download.geonames.org/export/dump/),但是當我使用Directory.GetFiles方法或DirectoryInfo di = new DirectoryInfo(ConfigurationManager.AppSettings["GeoFullDataURLPath"])我得到錯誤:獲取WINSERVICE通過URL目錄的文件名或WinForms應用程序

URI formats are not supported..

如何我可以解決這個問題?

+0

是匹配本地網址路徑? – jv42

+0

在遠程服務器上的URL,它不是本地路徑... –

回答

1

執行此任務的完整的源代碼是:

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)); 
} 
+0

感謝發佈,工作很好.. –

+0

永遠快樂 – 2011-12-12 17:27:49

2

您必須創建一個WebRequest。 DirectoryInfo僅用於本地驅動器和SMB共享,​​我相信。這應該是訣竅:http://www.csharp-examples.net/download-files/

+0

http://www.csharp-examples.net/download-files/在這個鏈接 - 如何下載文件,但我需要列出的所有文件的網址,不是如何下載.. –

+0

你將不得不解析列出文件的頁面的HTML,以確定每個目標的URL。 Shai的答案有一些可能有用的鏈接。 –

2

您不能在URL上使用Directory.GetFiles

請看下面的例子:

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

這將從URL下載文件file.zip以上。在網絡

目錄列表通常被阻止出於安全原因,

編輯: 見this

+0

我需要所有文件名(URL)的列表,在下載之前.. –

+1

@AlexeyZ。 AFAIK沒有內置的功能,你可以做到這一點,我想它的時間讓你的手髒(與當然代碼:) :) – Vamsi

相關問題