Marcus
Marcus

Reputation: 191

.NET How to compare two Strings that represent filenames ignoring case correctly

Given that (at least on NTFS) the filesystem on Windows is case insensitive, I would like to compare String fileA to String fileB as such:

fileA.Equals(fileB, StringComparison.CurrentCultureIgnoreCase)

The question then becomes which culture I should use, does the default current (ui?) culture suffice? I can't seem to find any BCL methods for this purpose.

Upvotes: 19

Views: 5327

Answers (6)

Guffa
Guffa

Reputation: 700592

You should use StringComparison.OrdinalIgnoreCase, according to Best Practices for Using Strings in the .NET Framework.

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

If you use a culture for matching the strings, you may get in a sitation where for example the names "häl.gif" and "hal.gif" would be considered a match.

Upvotes: 28

Hardik
Hardik

Reputation: 1764

I tried this.

Path.GetFullPath(path1).Equals(Path.GetFullPath(path2))

Upvotes: -1

Mihai Nita
Mihai Nita

Reputation: 5787

This is not possible to do reliably.

Yes, the case conversion for the file system is case-insensitive.

But the case conversion table is stored on the file system itself (for NTFS), and it does change between versions (for instance the Vista case conversion table was brought to the Unicode 5 level, so Vista NTFS and XP NTFS have different case conversion rules).

And the thing that matters is the OS that formatted the file system, not the current OS.

Then you can run into all kind of problems with other file systems (Mac OS does some kind of Unicode normalization (not the standard one)), Linux does not do anything, but Samba (implementing the Windows file sharing protocol) does. And has other tables than Windows.

So what happens if I map a letter to a network disk shared by Linux or Mac OS?

In general you should never try to compare file names. If you want to know if it is there, try to access it.

Upvotes: 3

Boaz
Boaz

Reputation: 26129

Marcus,

You might want to at look at the answer for another StackOverflow question, which is very similar: Win32 File Name Comparison , which in turn mentions http://www.siao2.com/2005/10/17/481600.aspx .

Following a link in another answer to the same question and digging further, I came across the following MSDN article http://msdn.microsoft.com/en-us/library/ms973919.aspx . It is worth a read in general, but when it comes to file name comparison it recommends using StringComparison.OrdinalIgnoreCase. See Table 1 in the article, which contains file paths as one of the data types handled or the following the quote:

So, when interpreting file names, cookies, or anything else where something like the å combination can appear, ordinal comparisons still offer the most transparent and fitting behavior.

Hopes this helps, Boaz

Upvotes: 2

Alejandro Díaz
Alejandro Díaz

Reputation: 36

You could use InvariantCulture (look at http://msdn.microsoft.com/en-us/library/4c5zdc6a.aspx).

In your example:


FileA.Equals(FileB,StringComparison.InvariantCultureIgnoreCase )

Upvotes: 0

kubal5003
kubal5003

Reputation: 7254

Maybe you could try this: http://msdn.microsoft.com/en-us/library/zkcaxw5y.aspx

Upvotes: 0

Related Questions