我不知道从哪里开始,我从模板管理代码。使用下面的代码,我可以从 Http 服务器端下载所有文件。它会检查这是否已经下载,如果是,那么它不会从站点中获取它。我只想下载部分文件。我正在尝试想一个简单的解决方案来实现以下几点:
- 获取 Server-Http 上的最后修改数据或最后创建时间。我了解如何从文件夹执行此操作,但我不想下载文件然后检查,我需要在服务器上执行此操作。Onlocal pc 将作为
FileInfo infoSource = new FileInfo(sourceDir);
,然后infoSource.CreationTime
sourceDir 是文件路径。http上可能有类似的东西? - 仅从服务器站点获取最新的 10 个文件。没有最新的,但最新的 10。
- 监视服务器站点,以便一旦将文件 MyFileName_Version 放在站点上,它将获取具有此命名约定的最新文件。
这些方法中的任何一种都对我有用,但我仍然是这些方法的新手,所以在这里挣扎。目前我有以下代码:
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
using Topshelf;
namespace AutomaticUpgrades
{
class Program
{
static void Main(string[] args)
{
// This path containts of the Site, Then binary-release/, The
string url = "HTTP://LOCALHOUST:1000000";
DownloadDataFromArtifactory(url);
}
private static void DownloadDataFromArtifactory(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string html = reader.ReadToEnd();
Regex regex = new Regex(GetDirectoryListingRegexForUrl(url));
MatchCollection matches = regex.Matches(html);
if (matches.Count > 0)
{
WebClient webClient = new WebClient();
foreach (Match match in matches)
{
if (match.Success)
{
Console.WriteLine(match.Groups["name"]);
//"C:\\Users\\RLEBEDEVS\\Desktop\\sourceFolder\\Http-server Download"
if (match.Groups["name"].Length > 5
&& DupeFile(match.Groups["name"].ToString(),
"C:\\Users\\RLEBEDEVS\\Desktop\\sourceFolder\\Http-server Download")
)
{
webClient.DownloadFile("HTTP://LOCALHOUST:1000000" + match.Groups["name"], "C:\\Users\\RLEBEDEVS\\Desktop\\sourceFolder\\Http-server Download\\" + match.Groups["name"]);
}
}
}
webClient.Dispose();
}
}
}
}
public static string GetDirectoryListingRegexForUrl(string url)
{
if (url.Equals("HTTP://LOCALHOUST:1000000"))
{
return "<a href=\".*\">(?<name>.*)</a>";
}
throw new NotSupportedException();
}
private static bool DupeFile(string httpFile, string folderLocation)
{
string[] files = System.IO.Directory.GetFiles(folderLocation);
foreach (string s in files)
{
if (System.IO.Path.GetFileName(s).ToString() == httpFile)
{
return false;
}
}
return true;
}
}
}