[英]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.