【发布时间】:2015-08-08 04:48:01
【问题描述】:
我需要创建一个方法来在网站(Hudson 服务器)上找到最新版本的应用程序并允许下载它。
到目前为止,我使用正则表达式扫描所有 HTML 并找到 href 标记并搜索我想要的字符串。
我想知道是否有最简单的方法。 我附上了我今天使用的代码:
namespace SDKGui
{
public struct LinkItem
{
public string Href;
public string Text;
public override string ToString()
{
return Href;
}
}
static class LinkFinder
{
public static string Find(string file)
{
string t=null;
List<LinkItem> list = new List<LinkItem>();
// 1.
// Find all matches in file.
MatchCollection m1 = Regex.Matches(file, @"(<a.*?>.*?</a>)",
RegexOptions.Singleline);
// 2.
// Loop over each match.
foreach (Match m in m1)
{
string value = m.Groups[1].Value;
LinkItem i = new LinkItem();
// 3.
// Get href attribute.
Match m2 = Regex.Match(value, @"href=\""(.*?)\""",
RegexOptions.Singleline);
if (m2.Success)
{
i.Href = m2.Groups[1].Value;
}
// 4.
// Remove inner tags from text.
t = Regex.Replace(value, @"\s*<.*?>\s*", "",
RegexOptions.Singleline);
if (t.Contains("hms_sdk_tool_"))
{
i.Text = t;
list.Add(i);
break;
}
}
return t;
}
}
}
【问题讨论】:
-
你的尝试有什么问题?
-
为什么不直接链接到应用程序中的静态 URL,该页面会将用户重定向到最新版本。类似 www.mysoftware.com/latest
-
对不起,我忘了注意,新版本完成后下载文件的名称会改变
-
@almog50:您是否考虑过使用HtmlAgilityPack 来收集
href值?如果是,我可以提供一个答案,说明如何做到这一点。
标签: c# regex winforms webclient-download