0

このコードは、保存したテキストファイルからフォルダディレクトリを読み取り、その'activeFilepath'変数を使用してフォルダ内の各ファイルを読み取り、名前を取得することを目的としています。これを使用して、振り向く。

StreamReader textIn = new StreamReader(imageSet);
try
{
    //Get the folder location by reading in the specific text file.
    activeFilepath = textIn.ReadLine();
    //If the user isn't choosing a password then go into the 'Blurred' directory of the folder.
    if (choosePassword == false)
       activeFilepath = activeFilepath + @"\Blurred";

    int i = 0;
    //Read in all of the image location strings, and put them in the pictureLocations list.
    foreach (string filePath in Directory.EnumerateFiles(activeFilepath))
    {
        pictureLocations.Add(filePath);
        //pictureLocations[i] = filePath;

        // Display an error image if there is a blank entry in the picture location.
        if (pictureLocations[i] == null)
            pictureLocations[i] = @"C:\Users\Public\Pictures\Sample Pictures\error.png";
        //Remove Thumbs.db from the list if read in.
        if (pictureLocations[i] == activeFilepath + @"\Thumbs.db")
        {
            pictureLocations.Remove(pictureLocations[i]);
        }

        i++;
    }

    foreach (PictureBox imagePanel in pictureBoxList)
    {
        int rand = randomGenerator.Next(0, pictureLocations.Count);
        //The images are then loaded in a random order from the picture location list.
        imagePanel.Load(pictureLocations[rand]);

        //Remove the picture just loaded out of the list so it won't repeat.
        pictureLocations.Remove(pictureLocations[rand]); 
        imagePanel.SizeMode = PictureBoxSizeMode.StretchImage;
    }

現時点では、Thumbs.dbをリストから削除すると(フォームにある画像ボックスにロードしたくないため)、インデックスが範囲外であるというインデックスタイプのエラーが表示されますが、単に読み込ませるだけで、代わりにパラメータタイプのエラーが発生します。

後者はThumbs.dbを画像として読み込もうとしているためだと思いますが、そもそも読み取りを削除または停止する別の方法があるのでしょうか。それはまったく別のものですか?

4

2 に答える 2

2

列挙から.dbを完全にフィルタリングしてみることができます。そうすれば、.dbを削除する必要はありません。

foreach (string filePath in Directory.EnumerateFiles(activeFilepath, "*.*",
                                                     SearchOption.AllDirectories)
                                                     .Where(x => Path.GetExtension(x) != ".db"))
{

}

コードは次のようになります。

List<string> enumerateFiles = Directory.EnumerateFiles(activeFilepath, "*.*", SearchOption.AllDirectories).ToList();

for (int i = 0; i < enumerateFiles.Count(); i++)
{
    String filePath = enumerateFiles[i];

    // skip the .db file
    if (Path.GetExtension(filePath) != ".db")
    {
        continue;                    
    }

    pictureLocations.Add(filePath);
    //pictureLocations[i] = filePath;

    // Display an error image if there is a blank entry in the picture location.
    if (pictureLocations[i] == null)
        pictureLocations[i] = @"C:\Users\Public\Pictures\Sample Pictures\error.png";

}
于 2012-08-10T14:29:54.497 に答える
2

クラッシュは、i何も追加しなくてもインクリメントすることです。簡単な修正は、アイテムを保持するつもりがない場合は、アイテムを追加しないことです。iそうすれば、厄介な変数は必要ありません。

//Read in all of the image location strings, and put them in the pictureLocations list.
foreach (string filePath in Directory.EnumerateFiles(activeFilepath))
{
    // Display an error image if there is a blank entry in the picture location.
    if (filePath == null)
        filePath = @"C:\Users\Public\Pictures\Sample Pictures\error.png";
    //Skip Thumbs.db
    if (filePath == activeFilepath + @"\Thumbs.db")
    {
        continue;
    }

    pictureLocations.Add(filePath);
}

つまり、アイテムを追加してから、追加するつもりがないことに気付く(そして、アイテムがどこにあるかを追跡する必要がある)のではなく、追加するかどうか(および必要かどうか)を事前に決定するだけです。編集する)、保持したい場合は追加します。

pictureLocationこれにより、との間の脆弱な関係が回避されiます。

于 2012-08-10T14:54:17.163 に答える