我在打开路径长度 > 256 的图像时遇到问题。我使用的是 Alphaleonis,它适用于我复制和查找文件,但在
System.IO.FileStream file = Alphaleonis.Win32.Filesystem.File.OpenRead(item)
当路径超过 256 个字符时,会抛出异常(“系统找不到指定的路径。”)。完整方法:
public static List<System.Drawing.Image> Find(string folderPath, string filenameOrPart)
{
List<System.Drawing.Image> imges = new List<System.Drawing.Image>();
try
{
List<string> filesInFolder = Alphaleonis.Win32.Filesystem.Directory.GetFiles(folderPath).ToList();
List<string> filesFound = filesInFolder.Where(f => f.Replace(folderPath, "").Contains(filenameOrPart)).ToList();
foreach (var item in filesFound)
{
int pathLen = item.Length;
try
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
using (System.IO.FileStream file = Alphaleonis.Win32.Filesystem.File.OpenRead(item))
{
byte[] bytes = new byte[file.Length];
int read;
while ((read = file.Read(bytes, 0, (int)file.Length)) > 0)
{
ms.Write(bytes, 0, read);
}
imges.Add(System.Drawing.Image.FromStream(ms));
}
}
catch (Exception e)
{
//dont add pic
}
}
}
catch (Exception)
{
//return empty list
}
return imges;
}
它适用于 pathLen < 257 但如果路径较长则进入内部捕获的图像。有什么建议么?先感谢您!
编辑:附加信息:图像位于网络驱动器上
Edit2:需要更新 Alphaleonis 并按照接受的答案调用该方法。