0

パス長が 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 を更新し、受け入れられた回答のようにメソッドを呼び出す必要がありました。

4

1 に答える 1

1

あなたのシナリオではよくわかりません。ただし、長さが 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");

テスト画像はフォルダ内に配置されます

于 2017-10-12T10:48:42.693 に答える