Does .net have a way to determine whether the local filesystem is case-sensitive?
File names: Traditionally, Unix-like operating systems treat file names case-sensitively while Microsoft Windows is case-insensitive but, for most file systems, case-preserving. For more details, see below.
Windows file system treats file and directory names as case-insensitive.
Yes the web. config file as well as Import directives and other portions of ASP.net are case sensitive.
Answer: A: No. MacOS is not a case sensitive file system by default. So you can't have two files named File.
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);
Here is the approach that does not use temporary files:
using System;
using System.Runtime.InteropServices;
static bool IsCaseSensitive()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ||
RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) // HFS+ (the Mac file-system) is usually configured to be case insensitive.
{
return false;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return true;
}
else if (Environment.OSVersion.Platform == PlatformID.Unix)
{
return true;
}
else
{
// A default.
return false;
}
}
Instead, it contains an ingrained knowledge about operating environments.
Readily available as NuGet package, works on everything .NET 4.0+ and updated on a regular basis: https://github.com/gapotchenko/Gapotchenko.FX/tree/master/Source/Gapotchenko.FX.IO#iscasesensitive
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With