c# file path string comparison case insensitivity
Asked Answered
H

4

4

I would like to compare two strings containing file paths in c#.

However, since in ntfs the default is to use case insensitive paths, I would like the string comparison to be case insensitive in the same way.

However I can't seem to find any information on how ntfs actually implements its case insensitivity. What I would like to know is how to perform a case insensitive comparison of strings using the same casing rules that ntfs uses for file paths.

Halfdan answered 7/10, 2014 at 7:41 Comment(1)
If you are really just looking to do a string comparison, below answers might work. However, no mere string comparison will tell you if two file paths refer to the same actual file object. For a c# function that will correctly tell you if two file paths refer to the same file, see https://mcmap.net/q/21981/-how-can-i-compare-directory-paths-in-c.Kingly
C
8

From MSDN:

The string behavior of the file system, registry keys and values, and environment variables is best represented by StringComparison.OrdinalIgnoreCase.

And:

When interpreting file names, cookies, or anything else where a combination such as "å" can appear, ordinal comparisons still offer the most transparent and fitting behavior.

Therefore it's simply:

String.Equals(fileNameA, fileNameB, StringComparison.OrdinalIgnoreCase)

(I always use the static Equals call in case the left operand is null)

Concertize answered 7/10, 2014 at 7:47 Comment(1)
I realize the question simply asked how to compare two file names in a case-insensitive manner, but one might guess that the purpose of such a comparison is to try to determine if two file names refer to the same object. If you are looking to determine if two file names refer to the same file, this will not do the trick. It should be pointed out that it is incredibly difficult to determine if two file names refer to the same file object, given the possibility of junctions, links, shares on networks, etc. See https://mcmap.net/q/21981/-how-can-i-compare-directory-paths-in-c.Kingly
P
1

While comparison of paths the path's separator direction is also very important. For instance:

 bool isEqual = String.Equals("myFolder\myFile.xaml", "myFolder/myFile.xaml", StringComparison.OrdinalIgnoreCase);

isEqual will be false.

Therefore needs to fix paths first:

 private string FixPath(string path)
    {
        return path.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
                   .ToUpperInvariant();
    }

Whereas this expression will be true:

bool isEqual = String.Equals(FixPath("myFolder\myFile.xaml"), FixPath("myFolder/myFile.xaml"), StringComparison.OrdinalIgnoreCase);
Pietism answered 23/8, 2016 at 13:5 Comment(0)
B
0
string path1 = "C:\\TEST";
string path2 = "c:\\test";

if(path1.ToLower() == path2.ToLower())
    MessageBox.Show("True");

Do you mean this or did i not get the question?

Bugbear answered 7/10, 2014 at 7:48 Comment(3)
While that works well for English characters for sure, I still don't know how windows actually handles international characters. That is my issue. If this were the case it would have been better to use String.Compare(string,string,StringComparison) as that would not instantiate two new strings. I want to be sure that the string comparison uses the same casing rules as ntfs.Halfdan
hm i am pretty sure that both tolower and strongcomparison work with any culture, as in they try to put it to lower case if lower case exists in that language. Copy paste some foreign typesets into strings and test it that wayBugbear
I know it works, but you misunderstood the question. I already know how to do a case insensitive search. What I asked was which case insensitivity rules should I use. The question has been answered above.Halfdan
F
0

I would go for

string.Compare(path1, path2, true) == 0

or if you want to specify cultures:

string.Compare(path1, path2, true, CultureInfo.CurrentCulture) == 0 

using ToUpper does a useless memory allocation every time you compare something

Freaky answered 7/10, 2014 at 7:52 Comment(1)
I know how to compare two strings case insensitively. What I want to be sure of is that the case insensitivity uses the same casing rules as NTFS. Does ntfs use the current culture?Halfdan

© 2022 - 2024 — McMap. All rights reserved.