我想从URL下载文件,文件名经常更新 对于E.g。:filename_Date.zip,其中日期发生变化。
以下是我使用的查询
WebClient webClient = new WebClient(); webClient.DownloadFile(" http://nppes.viva-it.com/NPPES_Deactivated_NPI_Report_081214.zip",@" C:\ Users \ gnanasem \ Documents \ NPIMatcher \ NPI.zip");
网址的目标网页:http://nppes.viva-it.com/NPI_Files.html 在这里你可以看到多个文件,我想下载第一个文件。
答案 0 :(得分:0)
这样做的一种方法是:
我得到了这样的工作:
using (var webClient = new WebClient())
{
var websiteHtml = webClient.DownloadString("http://nppes.viva-it.com/NPI_Files.html");
var urlPattern = "href\\s*=\\s*(?:[\"'](?<1>[^\"']*)[\"']|(?<1>\\S+))";
Match match = Regex.Match(websiteHtml, urlPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled, TimeSpan.FromSeconds(1));
var urlToDownload = string.Empty;
while (match.Success)
{
var urlFound = match.Groups[1].Value;
if (urlFound.ToLower().Contains("deactivated"))
{
urlToDownload = urlFound;
break;
}
match = match.NextMatch();
}
webClient.DownloadFile(urlToDownload, @"C:\Users\gnanasem\Documents\NPIMatcher\NPI.zip");
}