C# 通过WinService或WinForms应用程序中的URL目录获取文件名
我有win服务,它必须按URL下载所有zip文件(例如),但当我使用C# 通过WinService或WinForms应用程序中的URL目录获取文件名,c#,.net,windows-services,io,C#,.net,Windows Services,Io,我有win服务,它必须按URL下载所有zip文件(例如),但当我使用Directory.GetFiles方法或DirectoryInfo di=new DirectoryInfo(ConfigurationManager.AppSettings[“GeoFullDataURLPath]”)时,我得到错误: 不支持URI格式 如何解决此问题?您必须提出Web请求。DirectoryInfo仅适用于本地驱动器和SMB共享。这就应该做到了:你必须提出一个WebRequest。DirectoryInfo
Directory.GetFiles
方法或DirectoryInfo di=new DirectoryInfo(ConfigurationManager.AppSettings[“GeoFullDataURLPath]”)时,
我得到错误:
不支持URI格式
如何解决此问题?您必须提出Web请求。DirectoryInfo仅适用于本地驱动器和SMB共享。这就应该做到了:你必须提出一个WebRequest。DirectoryInfo仅适用于本地驱动器和SMB共享。这应该可以做到:您不能在URL上使用
目录.GetFiles
考虑以下示例:
WebClient webClient = new WebClient();
webClient.DownloadFile("http://download.geonames.org/export/dump/file.zip", "new-file.zip");
这将从上面的URL下载file.zip文件
由于安全原因,通常会阻止web上的目录列表
编辑:
请参见您不能在URL上使用
目录.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));
}
字符串
storeLocation=“C:\\dump”,
fileName=“”,
baseURL=”http://download.geonames.org/export/dump/";
WebClient r=新的WebClient();
string content=r.DownloadString(baseURL);
foreach(正则表达式中的匹配m.Matches(content,“”)
{
fileName=Regex.Match(m.Value,“\\w+\\.zip”).Value;
r、 下载文件(baseURL+文件名,Path.Combine(storeLocation,fileName));
}
执行此任务的完整源代码是:
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));
}
字符串
storeLocation=“C:\\dump”,
fileName=“”,
baseURL=”http://download.geonames.org/export/dump/";
WebClient r=新的WebClient();
string content=r.DownloadString(baseURL);
foreach(正则表达式中的匹配m.Matches(content,“”)
{
fileName=Regex.Match(m.Value,“\\w+\\.zip”).Value;
r、 下载文件(baseURL+文件名,Path.Combine(storeLocation,fileName));
}
URL是否匹配本地路径?远程服务器上的URL不是本地路径…URL是否匹配本地路径?远程服务器上的URL不是本地路径…@AlexeyZ。既然没有内置的函数可以实现这一点,我想是时候让你的手变脏了(当然还有代码):@AlexeyZ。如果没有内置的功能,你可以做到这一点,我想是时候让你的手脏了(当然与代码):)在这个链接-如何下载文件,但我需要的是所有文件的url列表,而不是如何下载..你将不得不解析网页的HTML列出文件,以确定每个目标的url。Shai的答案与一些可能有帮助的东西有关。在这个链接中-如何下载文件,但我需要的是所有文件的url列表,而不是如何下载..你必须解析列出文件的页面的HTML,以确定每个目标的url。Shai的答案与一些可能有帮助的东西有关。