Home > Software engineering >  What's the difference between GetFullPath(".") and Directory.GetCurrentDirectory()?
What's the difference between GetFullPath(".") and Directory.GetCurrentDirectory()?

Time:12-09

It might be a trivial question but I'm trying to understand the difference between these two different APIs used in this case. 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:

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?

CodePudding user response:

To add to Igor's response, you can check the .net source code:

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

Basically Directory.GetCurrentDirectory() does this (simplified):

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:

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

After lots of checks, it basically ends with LongPathHelper.Normalize(path).

So I would just use the first option, as it also communicates better your intentions.

  •  Tags:  
  • c#
  • Related