当路径长度>256 时,Alphaleonis File.OpenRead() 抛出 "The system cannot find the path specified."
Alphaleonis File.OpenRead() throws "The system cannot find the path specified." when pathlength>256
我在打开路径长度 > 256 的图像时遇到问题。我正在使用 Alphaleonis,它适用于我复制和查找文件,但在
System.IO.FileStream file = Alphaleonis.Win32.Filesystem.File.OpenRead(item)
当路径超过 256 个字符时会抛出异常 ("The system cannot find the path specified.")。完整方法:
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 并按照接受的答案调用方法。
我不确定你的情况。但是,要读取长度超过 256 的文件的详细信息,请尝试 Path.Combine(@"\?\", "your folder path")
我试过下面的代码,文件夹长度超过 300 并且有效
string path = "C:\1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234";
int filesCountOnShareDrive = Directory.GetFiles(Path.Combine(@"\?\", path))
.Where(x => new FileInfo(x).CreationTime.Date == DateTime.Today.Date).Count();
更新:
我不知道为什么它不适合你。但是,下面的代码对我来说很好
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 = 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 = 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;
}
调用下面的部分
string path = "C:\1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234";
Find(Path.Combine(@"\?\", path), "Test");
测试图片放在文件夹内
我在打开路径长度 > 256 的图像时遇到问题。我正在使用 Alphaleonis,它适用于我复制和查找文件,但在
System.IO.FileStream file = Alphaleonis.Win32.Filesystem.File.OpenRead(item)
当路径超过 256 个字符时会抛出异常 ("The system cannot find the path specified.")。完整方法:
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 并按照接受的答案调用方法。
我不确定你的情况。但是,要读取长度超过 256 的文件的详细信息,请尝试 Path.Combine(@"\?\", "your folder path")
我试过下面的代码,文件夹长度超过 300 并且有效
string path = "C:\1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234";
int filesCountOnShareDrive = Directory.GetFiles(Path.Combine(@"\?\", path))
.Where(x => new FileInfo(x).CreationTime.Date == DateTime.Today.Date).Count();
更新:
我不知道为什么它不适合你。但是,下面的代码对我来说很好
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 = 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 = 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;
}
调用下面的部分
string path = "C:\1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234";
Find(Path.Combine(@"\?\", path), "Test");
测试图片放在文件夹内