I have a problem opening images with pathlength > 256. I'm using Alphaleonis, which works for me copying and finding files but on
System.IO.FileStream file = Alphaleonis.Win32.Filesystem.File.OpenRead(item)
when the path has more than 256 characters there is an exception thrown ("The system cannot find the path specified."). Complete Method:
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;
}
It works for images where pathLen < 257 but goes into inner catch if path is longer. Any suggestions? Thank you in advance!
Edit: Additional info: Images are on a Network drive
Edit2: Needed to update Alphaleonis and call the method as in accepted answer.
答案 0 :(得分:0)
我不确定你的情景。但是,要读取长度超过256的文件的详细信息,请尝试Path.Combine(@“\?\”,“您的文件夹路径”)
我尝试了以下代码,文件夹长度超过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");
测试图像放在文件夹
中