使用System.Net.WebClient.DownloadFile方法。我可以从指定的URI下载文件。
我的问题是,URI可能会定向到有效的本地文件:
我希望这些文件路径为无效的URI或强制WebClient仅访问Http / Ftp。
foreach(var link in inputs){
Download(link)
}
void Download(string source)
{
var uri = new URI(source);
//Stop me from downloading from local file
var response = webClient.DownloadData(uri);
}
您可以看一下Scheme的Uri
属性。
它:
http
,其路径类似于http://www.test.com/index.html
file
,其路径类似于c:\test.txt
InvalidOperationException
借助于IsAbsoluteUri属性,我认为可以这样测试有效的uri:
var scheme = uri.IsAbsoluteUri ? uri.Scheme : null; // can be put in a SchemeOrNull extension method
if (acceptedSchemes.Contains(scheme)) // acceptedSchemes contains "http", "ftp", ..
{
...