views:

502

answers:

5

Does .net have a way to determine whether the local filesystem is case-sensitive?

+1  A: 

Try creating a temporary file in all lowercase, and then check if it exists using uppercase.

ScottS
what about abc.xyz and aBc.xyz
masfenix
+7  A: 

There is no such a function in the .NET Class Library.

You can, however, roll out your own: Try creating a file with a lowercase name and then try to open it with the upparcase version of its name. Probably it is possible to improve this method, but you get the idea.

EDIT: You could actually just take the first file in the root directory and then check if both filename.ToLower() and filename.ToUpper() exist. Unfortunately it is quite possible that both uppercase and lowercase variants of the same file exist, so you should compare the FileInfo.Name properties of both the lowercase and uppercase variants to see if they are indeed the same or not. This will not require writing to the disk.

Obviously, this will fail if there are no files at all on the volume. In this case, just fall back to the first option (see Martin's answer for the implementation).

DrJokepu
This could fail, if the first existing file, exists in both lower- and uppercase variants on a case-sensitive filesystem.
M4N
Martin: true. I will modify my answer accordingly.
DrJokepu
+7  A: 

You can create a file in the temp folder (using lowercase filename), then check if the file exists (using uppercase filename), e.g:

string file = Path.GetTempPath() + Guid.NewGuid().ToString().ToLower();
File.CreateText(file).Close();
bool isCaseInsensitive = File.Exists(file.ToUpper());
File.Delete(file);
M4N
Good answer. There is an extremely small chance the GUID created contains none of 'abcdef'. It's not very likely but it could happen!
Mitch Wheat
How about similarly a creating a temp folder, creating a known lowercase filename in it, and then testing with the uppercase name?
Mitch Wheat
Good comment, Mitch (the first one). You could add "a" to the filename to be sure it contains at least one letter.
M4N
+1  A: 

Keep in mind that you might have multiple file systems with different casing rules. For example, the root filesystem could be case-sensitive, but you can have a case-insensitive filesystem (e.g. an USB stick with a FAT filesystem on it) mounted somewhere. So if you do such checks, make sure that you make them in the directory that you are going to access.

Also, what if the user copies the data from say a case-sensitive to a case-insensitive file system? If you have files that differ only by case, one of them will overwrite the other, causing data loss. When copying in the other direction, you might also run into problems, for example, if file A contains a reference to file "b", but the file is actually named "B". This works on the original case-insensitive file system, but not on the case-sensitive system.

Thus I would suggest that you avoid depending on whether the file system is case-sensitive or not if you can. Do not generate file names that differ only by case, use the standard file picker dialogs, be prepared that the case might change, etc.

oefe
A: 

It's not a .NET function, but the GetVolumeInformation and GetVolumeInformationByHandleW functions from the Windows API will do what you want (see yje lpFileSystemFlags parameter.

Paul Moore