views:

323

answers:

5

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.

A: 

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

kubal5003
That uses the current culture, so that's the same as what he already has.
Guffa
Yes, sorry, I found that out a little bit later.
kubal5003
+12  A: 

As you would probably want an exact match rather than a culturally controlled match, consider using StringComparison.OrdinalIgnoreCase instead.

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.

Guffa
Sadly this does not answerthe question.
Boaz
@Boaz: Well, tecnically you are right, as the exact question is what culture to use, while I suggest that no culture should be used.
Guffa
It does answer the question. No culture should be used. NTFS filename comparisons are ordinal.
Pavel Minaev
I tried to find an official statement on MSDN supporting this but couldn't. If anyone finds one please link to it.
romkyns
A: 

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

In your example:


FileA.Equals(FileB,StringComparison.InvariantCultureIgnoreCase )
Alejandro Díaz
Don't - this won't be consistent with NTFS rules. Use Ordinal comparison.
Pavel Minaev
+1  A: 

Marcus,

You might want to at look at the answer for another StackOverflow question, which is very similar: http://stackoverflow.com/questions/410502/win32-file-name-comparison , which in turn mentions http://blogs.msdn.com/michkap/archive/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

Boaz
A: 

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.

Mihai Nita