此代码旨在从我存储的文本文件中读取文件夹目录,并使用该“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 作为图像加载,但我想知道是否有另一种方法可以从一开始就删除或阻止它被读取?完全是另外一回事吗?