首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >检查是否提供了完整路径

检查是否提供了完整路径
EN

Stack Overflow用户
提问于 2011-04-06 18:41:36
回答 4查看 56.2K关注 0票数 112

有没有一种方法可以检查给定的路径是否为完整路径?现在我这样做:

代码语言:javascript
运行
复制
if (template.Contains(":\\")) //full path already given
{
}
else //calculate the path from local assembly
{
}

但是一定有更优雅的方法来检查这个吧?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-04-06 18:43:36

尝试使用System.IO.Path.IsPathRooted?它还为绝对路径返回true

代码语言:javascript
运行
复制
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"
票数 151
EN

Stack Overflow用户

发布于 2011-04-06 18:45:22

试一试

代码语言:javascript
运行
复制
System.IO.Path.IsPathRooted(template)

适用于UNC路径和本地路径。

例如。

代码语言:javascript
运行
复制
Path.IsPathRooted(@"\\MyServer\MyShare\MyDirectory")  // returns true
Path.IsPathRooted(@"C:\\MyDirectory")  // returns true
票数 14
EN

Stack Overflow用户

发布于 2015-09-19 20:34:58

这是个老问题,但还有一个更适用的答案。如果需要确保卷包含在本地路径中,可以像这样使用System.IO.Path.GetFullPath():

代码语言:javascript
运行
复制
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
}
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5565029

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档