有没有一种方法可以检查给定的路径是否为完整路径?现在我这样做:
if (template.Contains(":\\")) //full path already given
{
}
else //calculate the path from local assembly
{
}
但是一定有更优雅的方法来检查这个吧?
发布于 2011-04-06 18:43:36
尝试使用System.IO.Path.IsPathRooted
?它还为绝对路径返回true
。
System.IO.Path.IsPathRooted(@"c:\foo"); // true
System.IO.Path.IsPathRooted(@"\foo"); // true
System.IO.Path.IsPathRooted("foo"); // false
System.IO.Path.IsPathRooted(@"c:1\foo"); // surprisingly also true
System.IO.Path.GetFullPath(@"c:1\foo");// returns "[current working directory]\1\foo"
发布于 2011-04-06 18:45:22
试一试
System.IO.Path.IsPathRooted(template)
适用于UNC路径和本地路径。
例如。
Path.IsPathRooted(@"\\MyServer\MyShare\MyDirectory") // returns true
Path.IsPathRooted(@"C:\\MyDirectory") // returns true
发布于 2015-09-19 20:34:58
这是个老问题,但还有一个更适用的答案。如果需要确保卷包含在本地路径中,可以像这样使用System.IO.Path.GetFullPath():
if (template == System.IO.Path.GetFullPath(template))
{
; //template is full path including volume or full UNC path
}
else
{
if (useCurrentPathAndVolume)
template = System.IO.Path.GetFullPath(template);
else
template = Assembly.GetExecutingAssembly().Location
}
https://stackoverflow.com/questions/5565029
复制相似问题