views:

40

answers:

3

I want to know that Filename1, provided by the user is the same as stored in DB (Filename2).

I was about to use

string.Equals(Filename1, Filename2, StringComparison.CurrentCultureIgnoreCase)

but then I doubted whether I should use StringComparison.InvariantCultureIgnoreCase.

Obviously, I need to do this the same way OS does, or use appropriate API.

In some cultures, AFAIK, characters (e.g. vocals) may change if the next one is Capitalized.

As I primarily target English-speaking market, I'd like my software to work well throughout the world.

A: 

If the files are actually existing on disc (which I assume) you can canonicalize the file name using Directory.GetFiles() and then compare the canonical file name:

string fileName1 = @"C:\someFolder/sOmeFiLE.txt";
string fileName2 = @"C:\somefolder\someFile.txt";

string canonicalFileName1 = Directory.GetFiles(
        Path.GetDirectoryName(fileName1), 
        Path.GetFileName(fileName1))[0];

string canonicalFileName2 = Directory.GetFiles(
        Path.GetDirectoryName(fileName2), 
        Path.GetFileName(fileName2))[0];

bool isEqual = string.Compare(canonicalFileName1, canonicalFileName2) == 0;
0xA3
+1  A: 

Yes, you should use the invariant culture. I would probably do it without case sensitivity as I'm sure that won't effect the invariant culture (otherwise it wouldn't be a lot of good).

If you consider (I don't know if this is true), that your database was made on a different machine using a different culture, not using the invariant comparison would cause you problems.

If you don't ignore the case, well then if I rename my 'Windows' folder to 'windows' you would say my files within their don't exist anymore, when obviously they do.

Ian
+1  A: 

To quote Michael Kaplan:

If you are trying to compare symbolic identifiers or operating system objects like filenames or named pipes, use an Ordinal type method (or an OrdinalIgnoreCase type method).

In other words, use StringComparison.OrdinalIgnoreCase as the argument to string.Equals to mimic the Windows file system.

Emperor XLII