为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
德玛西亚99 2022-11-22 16:00:15
我可以转换pathAndFilename为小写,但我需要一种方法来区分OpenRead大小写。// pathAndFileName has been converted with .ToLower()using (FileStream fileStream = File.OpenRead(pathAndFileName)){    Bitmap bitmap = new Bitmap(fileStream);    Image image = (Image)bitmap;}
查看完整描述

3 回答

?
子衿沉夜

TA贡献1828条经验 获得超3个赞

如果您尝试访问运行 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;

}


查看完整回答
反对 回复 2022-11-22
?
芜湖不芜

TA贡献1796条经验 获得超7个赞

我受到包含 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;

}


查看完整回答
反对 回复 2022-11-22
?
慕容708150

TA贡献1831条经验 获得超4个赞

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


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;

}


查看完整回答
反对 回复 2022-11-22
  • 3 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信