c# - "Path.GetFullPath"和网络路径

标签 c# .net path

为什么“Path.GetFullPath”在用网络路径上的相关元素解析路径时表现异常? 试试这个小例子并比较结果:

using System;
using System.IO;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine(Path.GetFullPath(@"C:\Stay\Elim1\Elim2\..\..\SomeFolder"));  // yields C:\Stay\SomeFolder
            Console.WriteLine(Path.GetFullPath(@"\\Stay\Elim1\Elim2\..\..\SomeFolder"));   // yields \\Stay\Elim1\SomeFolder ???
        }
    }
}

这可能是一个错误,或者其中可能有某种意义,但我不明白。

(所有路径甚至部分路径都不存在于我的机器上,因此它只是一个字符串操作)

最佳答案

当您使用网络路径时,路径的第二部分是共享名而不是目录。

Console.WriteLine(Path.GetFullPath(@"C:\SomeDir\Dir1\Dir2\..\..\SomeFolder"));  

C:\SomeDir\SomeFolder

Console.WriteLine(Path.GetFullPath(@"\\Server\ShareName\Dir1\Dir2\..\..\SomeFolder"));

\Server\ShareName\SomeFolder

关于c# - "Path.GetFullPath"和网络路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17838970/

相关文章:

node.js - 从 Node.js 中的另一个自定义模块访问自定义模块的正确路径表示法

html - 将文件导入 CSS 文档不起作用

c# - 如何在 C# Windows 应用商店应用程序(也称为 WinRT/Metro)中创建查询字符串?

.net - Directory.GetFiles 返回重复的文件名

C# .Net 解析 XML 字符串

c# - 使用 DotNetOpenAuth 从 OpenID 提供商获取电子邮件地址

windows - 批处理文件 : Error in relative path , 从当前目录向上一级

c# - 替换 databinder.eval 中的字符串

c# - 在 STA 下运行 NUnit 测试的问题

c# - linq to XML 作为 gridview 数据源