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

我可以转换pathAndFilename为小写,但我需要一种方法来区分OpenRead大小写。


// pathAndFileName has been converted with .ToLower()

using (FileStream fileStream = File.OpenRead(pathAndFileName))

{

    Bitmap bitmap = new Bitmap(fileStream);

    Image image = (Image)bitmap;

}


德玛西亚99
浏览 91回答 3
3回答

子衿沉夜

如果您尝试访问运行 Linux 或文件名区分大小写的其他操作系统的计算机上的文件,解决方法可能是(未测试!)使用您拥有的文件名作为模式来列出目录中的文件。请注意,可能有多个文件具有相同的名称,只是不同的拼写形式。在这种情况下,此辅助函数将引发异常。static void Main(string[] args){&nbsp; &nbsp; string pathAndFileName = ..your file name...;&nbsp; &nbsp; string resultFileName = GetActualCaseForFileName(pathAndFileName);&nbsp; &nbsp; using (FileStream fileStream = File.OpenRead(resultFileName))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Bitmap bitmap = new Bitmap(fileStream);&nbsp; &nbsp; &nbsp; &nbsp; Image image = (Image)bitmap;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; Console.WriteLine(resultFileName);}private static string GetActualCaseForFileName(string pathAndFileName){&nbsp; &nbsp; string directory = Path.GetDirectoryName(pathAndFileName);&nbsp; &nbsp; string pattern = Path.GetFileName(pathAndFileName);&nbsp; &nbsp; string resultFileName;&nbsp; &nbsp; // Enumerate all files in the directory, using the file name as a pattern&nbsp; &nbsp; // This will list all case variants of the filename even on file systems that&nbsp; &nbsp; // are case sensitive&nbsp; &nbsp; IEnumerable<string> foundFiles = Directory.EnumerateFiles(directory, pattern);&nbsp; &nbsp; if (foundFiles.Any())&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (foundFiles.Count() > 1)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // More than two files with the same name but different case spelling found&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception("Ambiguous File reference for " + pathAndFileName);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultFileName = foundFiles.First();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; throw new FileNotFoundException("File not found" + pathAndFileName, pathAndFileName);&nbsp; &nbsp; }&nbsp; &nbsp; return resultFileName;}

芜湖不芜

我受到包含 GetActualCaseForFileName() 的答案的启发,但它不适用于我的 Xamarin iOS 项目。我创建了以下代码,这似乎对我有用:public string getCaseNudgedPathName( string origPath) {&nbsp; &nbsp; var retPath = origPath;&nbsp; &nbsp; var dir = Path.GetDirectoryName( origPath);&nbsp; &nbsp; var pattern = Path.GetFileName( origPath);&nbsp; &nbsp; var foundFiles = Directory.GetFiles(dir);&nbsp; &nbsp; int countMatch = 0;&nbsp; &nbsp; foreach (var foundFile in foundFiles) {&nbsp; &nbsp; &nbsp; &nbsp; if ( foundFile.Equals(origPath,IgnoreCase)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; countMatch++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retPath = foundFile;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (countMatch > 1) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; throw new Exception( $"Ambiguous filename '{pattern}'");&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; return retPath;}

慕容708150

对于区分大小写的文件和目录,您需要结合第一个答案和:private static string GetActualCaseForFileName(string pathAndFileName){&nbsp; &nbsp; string directory = Path.GetDirectoryName(pathAndFileName);&nbsp; &nbsp; string directoryCaseSensitive = GetDirectoryCaseSensitive(directory)&nbsp; &nbsp; ...}private static string GetDirectoryCaseSensitive(string directory){&nbsp; var directoryInfo = new DirectoryInfo(directory);&nbsp; if (directoryInfo.Exists)&nbsp; {&nbsp; &nbsp; return directory;&nbsp; }&nbsp; if (directoryInfo.Parent == null)&nbsp; {&nbsp; &nbsp; return null;&nbsp; }&nbsp; var parent = GetDirectoryCaseSensitive(directoryInfo.Parent.FullName);&nbsp; if (parent == null)&nbsp; {&nbsp; &nbsp; return null;&nbsp; }&nbsp; return new DirectoryInfo(parent).GetDirectories(directoryInfo.Name, new EnumerationOptions {MatchCasing = MatchCasing.CaseInsensitive}).FirstOrDefault()?.FullName;}
打开App,查看更多内容
随时随地看视频慕课网APP