简体   繁体   English

GetFullPath(".") 和 Directory.GetCurrentDirectory() 有什么区别?

[英]What's the difference between GetFullPath(".") and Directory.GetCurrentDirectory()?

It might be a trivial question but I'm trying to understand the difference between these two different APIs used in this case.这可能是一个微不足道的问题,但我试图了解在这种情况下使用的这两种不同 API 之间的区别。 It seems to be that they are identical.似乎它们是相同的。

I wrote a quick testing program and looked at the returned value in debug mode, and value returned from these two APIs are identical:我写了一个快速测试程序并在调试模式下查看返回值,这两个 API 返回的值是相同的:

var result = Path.GetFullPath(".");               -> ..\source\\repos\\TestingApp\\TestingAppDotNet\\bin\\Debug
string path = Directory.GetCurrentDirectory();    -> ..\source\\repos\\TestingApp\\TestingAppDotNet\\bin\\Debug

Is this more like a personal preference thing?这更像是个人喜好吗?

To add to Igor's response, you can check the .net source code:要添加到 Igor 的响应中,您可以查看 .net 源代码:

https://referencesource.microsoft.com/#mscorlib/system/io/directory.cs,eebef077ff3930e1 https://referencesource.microsoft.com/#mscorlib/system/io/directory.cs,eebef077ff3930e1

Basically Directory.GetCurrentDirectory() does this (simplified):基本上Directory.GetCurrentDirectory()这样做(简化):

buffer = Win32Native.GetCurrentDirectoryW();
if (buffer.Contains('~'))
    return LongPathHelper.GetLongPathName(buffer);
return buffer.ToString();

While Path.GetFullPath() does a LOT more, you can check it here:虽然Path.GetFullPath()做的更多,但您可以在这里查看:

https://referencesource.microsoft.com/#mscorlib/system/io/path.cs,ecfb67b37299beba https://referencesource.microsoft.com/#mscorlib/system/io/path.cs,ecfb67b37299beba

After lots of checks, it basically ends with LongPathHelper.Normalize(path) .经过大量检查,它基本上以LongPathHelper.Normalize(path)结束。

So I would just use the first option, as it also communicates better your intentions.所以我只会使用第一个选项,因为它也能更好地传达您的意图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Environment.CurrentDirectory 和 Directory.GetCurrentDirectory 有什么区别? - What is the difference between Environment.CurrentDirectory and Directory.GetCurrentDirectory? Directory.GetCurrentDirectory - Directory.GetCurrentDirectory System.CurrentDomain.AppDomain.BaseDirectory和Directory.GetCurrentDirectory()之间的C#差异 - C#-Difference between System.CurrentDomain.AppDomain.BaseDirectory and Directory.GetCurrentDirectory() Directory.GetCurrentDirectory()不能在linux上运行? - Directory.GetCurrentDirectory() not working on linux? 将转义序列添加到Directory.GetCurrentDirectory() - adding escape sequence to Directory.GetCurrentDirectory() WPF设置中的默认值为“ Directory.GetCurrentDirectory”。 - Default value in WPF Settings as `Directory.GetCurrentDirectory` 无法将文件写入 Directory.GetCurrentDirectory() 中的路径 - Unable to write file to path in Directory.GetCurrentDirectory() C#Directory.GetCurrentDirectory()返回带有NUnit的system32 - C# Directory.GetCurrentDirectory() returning system32 with NUnit Directory.GetCurrentDirectory()根据命令行参数返回不同的结果 - Directory.GetCurrentDirectory() returns different results based on command line arguments C#:从浏览器启动时的Directory.getCurrentDirectory() - C#: Directory.getCurrentDirectory() when launching from Browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM