OS中的大小写敏感
默认情况下,文件系统中,
- linux 大小写敏感
- windows 和 MAC 系统大小写不敏感
但是 MAC 系统可以设置为大小写敏感
跨平台之殇
在编写跨平台程序的时候,对于文件系统大小写是否敏感的判定是很重要的,在这里,给出一个简单易行的办法。
基本思路就是:
- 首先生成一个全为大写的字符串作为文件名
FILE - 尝试在创建转化为小写的字符串命名的文件
file - 如果此时文件已存在,说明文件系统大小写不敏感;反之则大小写敏感
private static bool GetIsCaseSensitive()
{
// Fast pass for windows
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return false;
}
// https://github.com/dotnet/corefx/blob/bffef76f6af208e2042a2f27bc081ee908bb390b/src/Common/src/System/IO/PathInternal.CaseSensitivity.cs#L37
try
{
var pathWithUpperCase = Path.Combine(Path.GetTempPath(), "CASESENSITIVETEST" + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture));
using (new FileStream(pathWithUpperCase, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None, 0x1000, FileOptions.DeleteOnClose))
{
var lowerCased = pathWithUpperCase.ToLowerInvariant();
return !File.Exists(lowerCased);
}
}
catch (Exception exc)
{
// In case something goes terribly wrong, we don't want to fail just because
// of a casing test, so we assume case-insensitive-but-preserving.
Debug.Fail("Casing test failed: " + exc);
return false;
}
}