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

崩溃是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 回答
2

您可以尝试将 .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 回答