Does .net have a way to determine whether the local filesystem is case-sensitive?
相关问题
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- Is shmid returned by shmget() unique across proces
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
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.
Try creating a temporary file in all lowercase, and then check if it exists using uppercase.
How about this heuristic?
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.
There are actually two ways to interpret the original question.
This answer is based on the second interpretation, because I think that is what the OP wanted to know and also what matters to most people.
The following code is loosely based on M4N's and Nicolas Raoul's answer and attempts to create a really robust implementation that is able to determine whether the operating system handles file names case-sensitive inside of a specified directory (excluding sub-directories, since these could be mounted from another file system).
It works by creating two new files in succession, one with lower-case, the other one with upper-case characters. The files are locked exclusively and are deleted automatically when closed. This should avert any negative side effects caused by creating files. Of course, this implementation only works if the specified directory exists and the current user is able to create files inside of it.
The code is written for .NET Framework 4.0 and C# 7.2 (or later).
As you can see there is a tiny possibility for a race condition which can cause a false-negative. If this race condition is something you really worry about I suggest you do the check a second time when the result is false, either inside the
IsFileSystemCaseSensitive
method or outside of it. However, in my opinion, the probability of encountering this race condition once, let alone two times in a row, is astronomically small.I invoke The Cheat: