Arlodotexe / OwlCore.Storage

The most flexible file system abstraction, ever. Built in partnership with the UWP Community.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Incorrect folder check in SystemFolderWatcher

yoshiask opened this issue · comments

The problem

private static bool IsFolder(string path) => path.TrimEnd(Path.PathSeparator, Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) == Path.GetDirectoryName(path);

This method appears to check if a given path is a folder by stripping any trailing slashes and comparing that to Path.GetDirectoryName(path). Because Path.GetDirectoryName returns the path to the parent directory, this check will sometimes fail even for folders. For example:
image

Note this test will pass if the path does have a trailing slash, since in that case Path.GetDirectoryName will effectively return the root of an arbitrary subdirectory in the given path, which of course is always the original path.

The solution

The only reliable way to check if a path is folder is by asking the filesystem, calling Directory.Exists(path) like this:

private static bool IsFolder(string path) => Directory.Exists(path);

Remarks

The IsFile check has a similar issue; it will return true for both files and directory paths without trailing slashes.

commented

Good catch! Let's get this fixed.