我可以将pathAndFilename
转换为小写,但是就像我需要一种方法告诉OpenRead
不区分大小写。
// pathAndFileName has been converted with .ToLower()
using (FileStream fileStream = File.OpenRead(pathAndFileName))
{
Bitmap bitmap = new Bitmap(fileStream);
Image image = (Image)bitmap;
}
答案 0 :(得分:2)
如果您尝试在运行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;
}
答案 1 :(得分:1)
我的灵感来自包含 GetActualCaseForFileName() 的答案,但它不适用于我的 Xamarin iOS 项目。
我创建了以下代码,这似乎对我有用:
{
"node": {
"name": "v2.0.1",
"target": {
"oid": "85fe69df20062aafeea7593c45c1785f44208252"
}
}
}