如何确定文件系统在.net中是否区分大小写?

时间:2021-09-12 20:46:32

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

.net是否有办法确定本地文件系统是否区分大小写?

7 个解决方案

#1


12  

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

您可以在temp文件夹中创建一个文件(使用小写文件名),然后检查文件是否存在(使用大写文件名),例如:

string file = Path.GetTempPath() + Guid.NewGuid().ToString().ToLower();
File.CreateText(file).Close();
bool isCaseInsensitive = File.Exists(file.ToUpper());
File.Delete(file);

#2


7  

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

.NET类库中没有这样的功能。

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.

但是,您可以自己推出:尝试使用小写名称创建文件,然后尝试使用其名称的upparcase版本打开它。可能有可能改进这种方法,但你明白了。

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.

编辑:您实际上只需要获取根目录中的第一个文件,然后检查filename.ToLower()和filename.ToUpper()是否都存在。不幸的是,很可能存在同一文件的大写和小写变体,因此您应该比较小写和大写变体的FileInfo.Name属性,看它们是否确实相同。这不需要写入磁盘。

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).

显然,如果卷上根本没有文件,这将失败。在这种情况下,只需回到第一个选项(参见Martin对实现的回答)。

#3


5  

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.

请记住,您可能有多个具有不同大小写规则的文件系统。例如,根文件系统可能区分大小写,但您可以在某处安装不区分大小写的文件系统(例如,带有FAT文件系统的USB记忆棒)。因此,如果您进行此类检查,请确保将它们放在您要访问的目录中。

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.

此外,如果用户将数据从区分大小写复制到不区分大小写的文件系统,该怎么办?如果您的文件只有大小写不同,则其中一个文件将覆盖另一个文件,从而导致数据丢失。在向另一个方向复制时,您可能还会遇到问题,例如,如果文件A包含对文件“b”的引用,但该文件实际上名为“B”。这适用于原始不区分大小写的文件系统,但不适用于区分大小写的系统。

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.

因此,如果可以,我建议您避免依赖于文件系统是否区分大小写。不要生成仅根据大小写不同的文件名,使用标准文件选择器对话框,准备好案例可能会更改等。

#4


1  

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

尝试以全小写形式创建临时文件,然后使用大写检查它是否存在。

#5


1  

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.

它不是.NET函数,但Windows API中的GetVolumeInformation和GetVolumeInformationByHandleW函数可以执行您想要的操作(请参阅yje lpFileSystemFlags参数。

#6


0  

/// <summary>
/// Check whether the operating system is case-sensitive.
/// For instance on Linux you can have two files/folders called
//// "test" and "TEST", but on Windows the two can not coexist.
/// This method does not extend to mounted filesystems, which might have different properties.
/// </summary>
/// <returns>true if the operating system is case-sensitive</returns>
public static bool IsFileSystemCaseSensitive()
{
    // Actually try.
    string file = Path.GetTempPath() + Guid.NewGuid().ToString().ToLower() + "test";
    File.CreateText(file).Close();
    bool result = File.Exists(file.ToUpper());
    File.Delete(file);

    return result;
}

Based on M4N's answer, with the following changes:

根据M4N的答案,进行以下更改:

  • Static names so that we are sure it contains a letter and not only numbers.
  • 静态名称,以便我们确定它包含一个字母,而不仅仅是数字。
  • Maybe more readable?
  • 也许更具可读性?
  • Wrapped in a method.
  • 包裹在一个方法中。
  • Documentation.
  • 文档。

A better strategy would be to take a path as an argument, and create the file on the same filesystem, but writing there might have unexpected consequences.

更好的策略是将路径作为参数,并在同一文件系统上创建文件,但在那里写文件可能会产生意想不到的后果。

#7


0  

I invoke The Cheat:

我援引作弊:

Path.DirectorySeparatorChar == '\\' ? "I'm insensitive" : "I'm probably sensitive"

#1


12  

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

您可以在temp文件夹中创建一个文件(使用小写文件名),然后检查文件是否存在(使用大写文件名),例如:

string file = Path.GetTempPath() + Guid.NewGuid().ToString().ToLower();
File.CreateText(file).Close();
bool isCaseInsensitive = File.Exists(file.ToUpper());
File.Delete(file);

#2


7  

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

.NET类库中没有这样的功能。

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.

但是,您可以自己推出:尝试使用小写名称创建文件,然后尝试使用其名称的upparcase版本打开它。可能有可能改进这种方法,但你明白了。

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.

编辑:您实际上只需要获取根目录中的第一个文件,然后检查filename.ToLower()和filename.ToUpper()是否都存在。不幸的是,很可能存在同一文件的大写和小写变体,因此您应该比较小写和大写变体的FileInfo.Name属性,看它们是否确实相同。这不需要写入磁盘。

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).

显然,如果卷上根本没有文件,这将失败。在这种情况下,只需回到第一个选项(参见Martin对实现的回答)。

#3


5  

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.

请记住,您可能有多个具有不同大小写规则的文件系统。例如,根文件系统可能区分大小写,但您可以在某处安装不区分大小写的文件系统(例如,带有FAT文件系统的USB记忆棒)。因此,如果您进行此类检查,请确保将它们放在您要访问的目录中。

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.

此外,如果用户将数据从区分大小写复制到不区分大小写的文件系统,该怎么办?如果您的文件只有大小写不同,则其中一个文件将覆盖另一个文件,从而导致数据丢失。在向另一个方向复制时,您可能还会遇到问题,例如,如果文件A包含对文件“b”的引用,但该文件实际上名为“B”。这适用于原始不区分大小写的文件系统,但不适用于区分大小写的系统。

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.

因此,如果可以,我建议您避免依赖于文件系统是否区分大小写。不要生成仅根据大小写不同的文件名,使用标准文件选择器对话框,准备好案例可能会更改等。

#4


1  

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

尝试以全小写形式创建临时文件,然后使用大写检查它是否存在。

#5


1  

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.

它不是.NET函数,但Windows API中的GetVolumeInformation和GetVolumeInformationByHandleW函数可以执行您想要的操作(请参阅yje lpFileSystemFlags参数。

#6


0  

/// <summary>
/// Check whether the operating system is case-sensitive.
/// For instance on Linux you can have two files/folders called
//// "test" and "TEST", but on Windows the two can not coexist.
/// This method does not extend to mounted filesystems, which might have different properties.
/// </summary>
/// <returns>true if the operating system is case-sensitive</returns>
public static bool IsFileSystemCaseSensitive()
{
    // Actually try.
    string file = Path.GetTempPath() + Guid.NewGuid().ToString().ToLower() + "test";
    File.CreateText(file).Close();
    bool result = File.Exists(file.ToUpper());
    File.Delete(file);

    return result;
}

Based on M4N's answer, with the following changes:

根据M4N的答案,进行以下更改:

  • Static names so that we are sure it contains a letter and not only numbers.
  • 静态名称,以便我们确定它包含一个字母,而不仅仅是数字。
  • Maybe more readable?
  • 也许更具可读性?
  • Wrapped in a method.
  • 包裹在一个方法中。
  • Documentation.
  • 文档。

A better strategy would be to take a path as an argument, and create the file on the same filesystem, but writing there might have unexpected consequences.

更好的策略是将路径作为参数,并在同一文件系统上创建文件,但在那里写文件可能会产生意想不到的后果。

#7


0  

I invoke The Cheat:

我援引作弊:

Path.DirectorySeparatorChar == '\\' ? "I'm insensitive" : "I'm probably sensitive"