检查输入的路径是URL还是本地文件 [英] Check if the path input is URL or Local File
本文介绍了检查输入的路径是URL还是本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在xmldataprovider中工作,我们有配置值"source",该值可能是本地文件或url像
I'm working in xmldataprovider and we have configuration value "source" this value may be local file or url like
c:\ data \ test.xml-绝对data \ test.xml --relative
c:\data\test.xml --absolute data\test.xml --relative
或url http:\ mysite \ test.xml
or url http:\mysite\test.xml
如何确定代码中的所有这些情况我在用c#
how I can determine all this cases in code I'm working c#
推荐答案
private static bool IsLocalPath(string p)
{
return new Uri(p).IsFile;
}
...或者,如果您想包括对某些无效URI的支持...
...or, if you want to include support for certain invalid URIs...
private static bool IsLocalPath(string p)
{
if (p.StartsWith("http:\\"))
{
return false;
}
return new Uri(p).IsFile;
}
用法示例
static void Main(string[] args)
{
CheckIfIsLocalPath("C:\\foo.txt");
CheckIfIsLocalPath("C:\\");
CheckIfIsLocalPath("http://www.txt.com");
}
private static void CheckIfIsLocalPath(string p)
{
var result = IsLocalPath(p); ;
Console.WriteLine("{0} {1} {2}", result, p, new Uri(p).AbsolutePath);
}
这篇关于检查输入的路径是URL还是本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文