Search code examples
c#filepathsystem.drawingsystem.io.filepathtoolongexception

Alphaleonis File.OpenRead() throws "The system cannot find the path specified." when pathlength>256


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.


Solution

  • I am not sure with your scenario. But, to read details of files longer than 256 length, try Path.Combine(@"\?\", "your folder path")

    I tried below code, folder length is longer than 300 and it worked

    string path = "C:\\1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234";
            int filesCountOnShareDrive = Directory.GetFiles(Path.Combine(@"\\?\", path))
                                .Where(x => new FileInfo(x).CreationTime.Date == DateTime.Today.Date).Count();
    

    Updated:

    I do not know why it is not working for you. But, below code works fine for me

    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;
        }
    

    Calling part below

     string path = "C:\\1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234";
            Find(Path.Combine(@"\\?\", path), "Test");
    

    Test image is placed inside the folder