C# 检查路径输入是URL还是本地文件
我在xmldataprovider中工作,我们有配置值“source”,该值可能是本地文件或url 像 c:\data\test.xml--绝对值 data\test.xml——相对 或url http:\mysite\test.xml 如何在代码中确定所有这些情况 我在c#工作 …或者,如果要包括对某些无效URI的支持C# 检查路径输入是URL还是本地文件,c#,C#,我在xmldataprovider中工作,我们有配置值“source”,该值可能是本地文件或url 像 c:\data\test.xml--绝对值 data\test.xml——相对 或url http:\mysite\test.xml 如何在代码中确定所有这些情况 我在c#工作 …或者,如果要包括对某些无效URI的支持 private static bool IsLocalPath(string p) { if (p.StartsWith("http:\\")) { retur
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);
}
只需为所有远程文件设置规则,它应该(而且必须)在URI中包含协议:
http://
ftp://
对于本地文件,它可以是
file://
对于绝对路径,没有协议用于相对路径
然后简单的正则表达式可以帮助提取正确的信息。仅仅使用新Uri(yourPath)
在所有情况下都不起作用
比较各种场景(通过LinqPad)
结果
如果路径(不存在路径、空字符串、空字符串)Uri(p)格式不正确,IsFile将引发异常。 在我看来,最好使用两种方法来辨别什么是什么:
private bool PathIsLocalFile(string path)
{
return File.Exists(path);
}
private bool PathIsUrl(string path)
{
if (File.Exists(path))
return false;
try
{
Uri uri = new Uri(path);
return true;
}
catch (Exception)
{
return false;
}
}
//
返回:
如果调用方具有所需的权限且路径包含现有文件的名称,则为true;否则,错误。如果路径为null、无效路径或长度为零的字符串,此方法也会返回false。如果调用方没有足够的权限读取指定的文件,则不会引发异常,并且无论路径是否存在,该方法都返回false。
//
例外情况:
T:System.ArgumentNullException:
uriString为空
T:System.UriFormatException:
URI字符串为空。-或-URI字符串中指定的方案格式不正确。请参阅System.Uri.CheckSchemeName(System.String)。-或-Uri字符串包含太多斜杠。-或-Uri字符串中指定的密码无效。-或-Uri字符串中指定的主机名无效。-或-Uri字符串中指定的文件名无效-或-在URI字符串中指定的用户名无效。-或-在URI字符串中指定的主机或机构名称不能用反斜杠终止。-或-在URI字符串中指定的端口号无效或无法解析。-或-URI字符串的长度超过65519个字符。-或-在URI字符串中指定的方案的长度超过1023个字符字符。-或-uriString中存在无效字符序列。-或-uriString中指定的MS-DOS路径必须以c:\.开头。这是@drzaus的答案避免异常处理方法的一个版本。有一个可用的
var tests = new[] {
Path.GetTempFileName(),
Path.GetDirectoryName(Path.GetTempFileName()),
"http://in.ter.net",
"http://in.ter.net/",
"http://in.ter.net/subfolder/",
"http://in.ter.net/subfolder/filenoext",
"http://in.ter.net/subfolder/file.ext",
"http://in.ter.net/subfolder/file.ext?somequerystring=yes",
Path.GetFileName(Path.GetTempFileName()),
Path.Combine("subfolder", Path.GetFileName(Path.GetTempFileName()))};
tests.Select(test =>
{
if (Uri.TryCreate(test, UriKind.Absolute, out var u))
{
return new { test, u.IsAbsoluteUri, u.IsFile };
}
return new { test, IsAbsoluteUri = false, IsFile = true };
}
).Dump();
作为扩展方法:
公共静态bool IsLocalAbsolutePath(此字符串输入)
{
if(Uri.TryCreate(input,UriKind.Absolute,out-var-Uri))
{
返回uri.IsFile;
}
返回false;
}
公共静态bool isremoteabsolutionPath(此字符串输入)
{
if(Uri.TryCreate(input,UriKind.Absolute,out-var-Uri))
{
return!uri.IsFile;
}
返回false;
}
当我将此用于relative时,引发了以下异常运行时异常:System.UriFormatException-无效URI:无法确定URI的格式。@ahmed:对于http:\mysite\test.xml一个,对吗?那个不是有效的Uri。您可能必须向方法添加逻辑,以便使用类似于p.StartsWith(@“http:\”)
的条件来进行快捷操作。哦,是的,对不起http:\这是我的错误,它是http:\\
Microsoft docs:
public Uri(string uriString);
T:System.ArgumentNullException:
T:System.UriFormatException:
var tests = new[] {
Path.GetTempFileName(),
Path.GetDirectoryName(Path.GetTempFileName()),
"http://in.ter.net",
"http://in.ter.net/",
"http://in.ter.net/subfolder/",
"http://in.ter.net/subfolder/filenoext",
"http://in.ter.net/subfolder/file.ext",
"http://in.ter.net/subfolder/file.ext?somequerystring=yes",
Path.GetFileName(Path.GetTempFileName()),
Path.Combine("subfolder", Path.GetFileName(Path.GetTempFileName()))};
tests.Select(test =>
{
if (Uri.TryCreate(test, UriKind.Absolute, out var u))
{
return new { test, u.IsAbsoluteUri, u.IsFile };
}
return new { test, IsAbsoluteUri = false, IsFile = true };
}
).Dump();