在WinService或WinForms应用程序中通过URL目录获取文件名
本文关键字:URL 获取 文件名 WinService WinForms 应用程序 | 更新日期: 2023-09-27 18:17:28
我有win服务,必须通过URL下载所有zip文件(例如http://download.geonames.org/export/dump/),但当我使用Directory.GetFiles
方法或DirectoryInfo di = new DirectoryInfo(ConfigurationManager.AppSettings["GeoFullDataURLPath"])
时,我得到错误:
URI格式不支持…
我该如何解决这个问题?
你必须创建一个WebRequest。DirectoryInfo只适用于本地驱动器和SMB共享。这应该能奏效:http://www.csharp-examples.net/download-files/
不能在URL上使用Directory.GetFiles
考虑下面的例子:
WebClient webClient = new WebClient();
webClient.DownloadFile("http://download.geonames.org/export/dump/file.zip", "new-file.zip");
这将从上面的URL下载文件file.zip。
由于安全原因,web上的目录列表通常被阻止,
编辑:看到这个
执行此任务的完整源代码为:
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));
}