Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine whether the filesystem is case-sensitive in .net?

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

like image 845
Matt Avatar asked Jan 10 '09 00:01

Matt


People also ask

Which file systems are 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.

Are file path case-sensitive?

Windows file system treats file and directory names as case-insensitive.

Is .NET case-sensitive?

Yes the web. config file as well as Import directives and other portions of ASP.net are case sensitive.

Is Mac filesystem case-sensitive?

Answer: A: No. MacOS is not a case sensitive file system by default. So you can't have two files named File.


2 Answers

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);
like image 193
M4N Avatar answered Oct 18 '22 18:10

M4N


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

like image 40
ogggre Avatar answered Oct 18 '22 19:10

ogggre