在忽略路径和文件名中的大小写的同时打开文件

Open a file while ignoring case in the path and filename

我可以将 pathAndFilename 转换为小写,但这就像我需要一种方法来告诉 OpenRead 不区分大小写。

// pathAndFileName has been converted with .ToLower()
using (FileStream fileStream = File.OpenRead(pathAndFileName))
{
    Bitmap bitmap = new Bitmap(fileStream);
    Image image = (Image)bitmap;
}

如果您尝试访问计算机 运行 Linux 或文件名区分大小写的其他操作系统上的文件,解决方法可能是(未测试!)使用您的文件名有一个模式来列出目录中的文件。请注意,可能有多个文件具有相同的名称,只是不同的拼写形式。在这种情况下,此辅助函数将引发异常。

static void Main(string[] args)
{
    string pathAndFileName = ..your file name...;
    string resultFileName = GetActualCaseForFileName(pathAndFileName);

    using (FileStream fileStream = File.OpenRead(resultFileName))
    {
        Bitmap bitmap = new Bitmap(fileStream);
        Image image = (Image)bitmap;
    }    


    Console.WriteLine(resultFileName);
}

private static string GetActualCaseForFileName(string pathAndFileName)
{
    string directory = Path.GetDirectoryName(pathAndFileName);
    string pattern = Path.GetFileName(pathAndFileName);
    string resultFileName;

    // Enumerate all files in the directory, using the file name as a pattern
    // This will list all case variants of the filename even on file systems that
    // are case sensitive
    IEnumerable<string> foundFiles = Directory.EnumerateFiles(directory, pattern);

    if (foundFiles.Any())
    {
        if (foundFiles.Count() > 1)
        {
            // More than two files with the same name but different case spelling found
            throw new Exception("Ambiguous File reference for " + pathAndFileName);
        }
        else
        {
            resultFileName = foundFiles.First();
        }
    }
    else
    {
        throw new FileNotFoundException("File not found" + pathAndFileName, pathAndFileName);
    }

    return resultFileName;
}

我受到包含 GetActualCaseForFileName() 的答案的启发,但它不适用于我的 Xamarin iOS 项目。

我创建了以下代码,它似乎对我有用:

public string getCaseNudgedPathName( string origPath) {
    var retPath = origPath;
    var dir = Path.GetDirectoryName( origPath);
    var pattern = Path.GetFileName( origPath);

    var foundFiles = Directory.GetFiles(dir);
    int countMatch = 0;
    foreach (var foundFile in foundFiles) {
        if ( foundFile.Equals(origPath,IgnoreCase)) {
            countMatch++;
            retPath = foundFile;
        }
    }
    if (countMatch > 1) { 
        throw new Exception( $"Ambiguous filename '{pattern}'");
    } 
    return retPath;
}

对于区分大小写的文件和目录,您需要结合使用第一个答案和:

private static string GetActualCaseForFileName(string pathAndFileName)
{
    string directory = Path.GetDirectoryName(pathAndFileName);
    string directoryCaseSensitive = GetDirectoryCaseSensitive(directory)
    ...
}


private static string GetDirectoryCaseSensitive(string directory)
{
  var directoryInfo = new DirectoryInfo(directory);
  if (directoryInfo.Exists)
  {
    return directory;
  }

  if (directoryInfo.Parent == null)
  {
    return null;
  }

  var parent = GetDirectoryCaseSensitive(directoryInfo.Parent.FullName);
  if (parent == null)
  {
    return null;
  }

  return new DirectoryInfo(parent).GetDirectories(directoryInfo.Name, new EnumerationOptions {MatchCasing = MatchCasing.CaseInsensitive}).FirstOrDefault()?.FullName;
}