c# - 检查是否给出完整路径

标签 c# .net validation path

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

if (template.Contains(":\\")) //full path already given
{
}
else //calculate the path from local assembly
{
}

但是必须有更优雅的方法来检查这个吗?

最佳答案

尝试使用 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"

关于c# - 检查是否给出完整路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5565029/

相关文章:

C#/WinAPI - 监视内存区域

c# - 如何在不必声明泛型类型的情况下使用具有泛型的类

.net - 可移植类库版本控制

java - 如何在 Struts2 中验证空集合?

java - 使用 Java 中的 Xerces 针对 XSD 1.1 进行 XML 验证

c# - 如何将列表框选定的项目分配给源属性

c# - 捕获由 async void 方法抛出的异常

c# - 获取 Web 项目引用的类库项目中的相对文件路径

asp.net - .NET 的 HTML sanitizer

asp.net - 允许更广泛的输入范围后验证危险字符MVC模型字段的正确方法