Skip to content

Commit

Permalink
Add APIs to check if a given path exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarun047 committed Jan 26, 2022
1 parent 43ceed5 commit 81349ba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/IO/Path.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ public static partial class Path

public static char[] GetInvalidPathChars() => new char[] { '\0' };

// Checks if the given path is available for use.
public static bool Exists([NotNullWhen(true)] string? path)
{
if (path == null)
{
return false;
}

ReadOnlySpan<char> fullPath = GetFullPath(path).AsSpan();
return Interop.Sys.LStat(fullPath, out Interop.Sys.FileStatus _) > 0;
}

// Expands the given path to a fully qualified path.
public static string GetFullPath(string path)
{
Expand Down
13 changes: 13 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/IO/Path.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ public static partial class Path
(char)31
};

//Checks if the given path is available for use.
public static bool Exists([NotNullWhen(true)] string? path)
{
if (path == null)
{
return false;
}

var fullPath = GetFullPath(path);
Interop.Kernel32.WIN32_FILE_ATTRIBUTE_DATA data = default;
return FileSystem.FillAttributeInfo(fullPath, ref data, returnErrorOnNotFound: true) == 0;
}

// Expands the given path to a fully qualified path.
public static string GetFullPath(string path)
{
Expand Down

0 comments on commit 81349ba

Please sign in to comment.