簡體   English   中英

打開文件時忽略路徑和文件名中的大小寫

[英]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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM