简体   繁体   中英

Cross-platform filepaths comparison

I'm trying to check if two file path strings specify the same path.

We know paths are not case-sensitive in Windows.

Path.GetFullPath(path1).Equals(Path.GetFullPath(path2), StringComparison.CurrentCultureIgnoreCase)

I know it will not work correctly on Linux, cause paths are case-sensitive there. So I'm searching for some indication of platform case-sensitivity for paths. Or for some function like Path.Equals .

In your case, it's probably the easiest (and most reliable) to check if Path.DirectorySeparatorChar is equal to '/' or '\\' and if it's the former, call the same method without the "IgnoreCase" part.

In full:

if(Path.DirectorySeparatorChar == '/')
{
    Path.GetFullPath(path1).Equals(Path.GetFullPath(path2), StringComparison.CurrentCulture);
}
else
{
    Path.GetFullPath(path1).Equals(Path.GetFullPath(path2), StringComparison.CurrentCultureIgnoreCase);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM