我怎样才能先搜索特定目录的 gif 文件,然后继续搜索其余的文件?
How can i search first for a specific directory gif file and then continue to the rest?
private void timer1_Tick(object sender, EventArgs e)
{
DirectoryInfo dir1 = new DirectoryInfo(@"C:\Users\Chocolade\AppData\Local\SatellitesImagesDownloads\SatellitesImagesDownloads\Countries\Israel\");
fi = dir1.GetFiles("*.gif");
foreach (FileInfo finfo in fi)
{
if (fi.Length > 0 && finfo.Length > 0)
{
timer1.Enabled = false;
pictureBox1.Load(finfo.FullName);
listView1.Items[0].Checked = true;
}
}
}
这适用于目录 Israel。
计时器间隔设置为 100。
在 Israel 目录中找到 gif 文件后,它会将其加载到 pictureBox1
并检查 listView1
中的 CheckBox
是否有此项。
但现在我想更改它,让它搜索所有子目录,但首先只搜索 Israel。一旦它找到 Israel 并将 gif 加载到 pictureBox1
并选中 CheckBox
然后继续搜索所有其他子目录,当子目录中有 gif 文件时,选中 [=] 中的复选框14=].
listView1
中的项目是国家名称。
现在我更改了代码,但它还没有达到我想要的效果:
现在只在 Israel 目录中搜索,现在搜索 Countries 下的所有子目录。
但不确定如何从这里继续。
private void timer1_Tick(object sender, EventArgs e)
{
DirectoryInfo dir1 = new DirectoryInfo(@"C:\Users\Chocolade\AppData\Local\SatellitesImagesDownloads\SatellitesImagesDownloads\Countries\");
fi = dir1.GetFiles("*.gif", SearchOption.AllDirectories);
foreach (FileInfo finfo in fi)
{
if (fi.Length > 0 && finfo.Length > 0)
{
timer1.Enabled = false;
pictureBox1.Load(finfo.FullName);
listView1.Items[0].Checked = true;
}
}
}
您实际上已经用文字描述了您的算法:
but first only for Israel once it found Israel loaded the gif to the pictureBox1 and checked the checkbox
这里实际上不需要 for 循环,因为如果找到多个文件,您将加载最后一个文件,您可以使用 Last()
方法
DirectoryInfo dir1 = new DirectoryInfo(@"C:\Users\Chocolade\AppData\Local\SatellitesImagesDownloads\SatellitesImagesDownloads\Countries\Israel\");
fi = dir1.GetFiles("*.gif");
// if you have found any gif files at all
if (fi.Any())
{
timer1.Enabled = false;
pictureBox1.Load(fi.Last().FullName);
listView1.Items[0].Checked = true;
}
then continue and search all other sub directories and when there are gif file in a sub directory check the checkbox for it in the listView1.
fi = dir1.GetFiles("*.gif", SearchOption.AllDirectories);
// if you have found any gif files at all
if (fi.Any())
{
timer1.Enabled = false;
listView1.Items[0].Checked = true;
}
将这两个代码部分放入您的 timer1_Tick
活动中。
您也可以将它们组合成一个 if-else 语句,因为如果您在 Israel 文件夹中找到 gif 文件,您的复选框将被选中。
private void timer1_Tick(object sender, EventArgs e)
{
DirectoryInfo dir1 = new DirectoryInfo(@"C:\Users\Chocolade\AppData\Local\SatellitesImagesDownloads\SatellitesImagesDownloads\Countries\Israel\");
fi = dir1.GetFiles("*.gif");
foreach (FileInfo finfo in fi)
{
if (fi.Length > 0 && finfo.Length > 0)
{
timer1.Enabled = false;
pictureBox1.Load(finfo.FullName);
listView1.Items[0].Checked = true;
}
}
}
这适用于目录 Israel。
计时器间隔设置为 100。
在 Israel 目录中找到 gif 文件后,它会将其加载到 pictureBox1
并检查 listView1
中的 CheckBox
是否有此项。
但现在我想更改它,让它搜索所有子目录,但首先只搜索 Israel。一旦它找到 Israel 并将 gif 加载到 pictureBox1
并选中 CheckBox
然后继续搜索所有其他子目录,当子目录中有 gif 文件时,选中 [=] 中的复选框14=].
listView1
中的项目是国家名称。
现在我更改了代码,但它还没有达到我想要的效果:
现在只在 Israel 目录中搜索,现在搜索 Countries 下的所有子目录。
但不确定如何从这里继续。
private void timer1_Tick(object sender, EventArgs e)
{
DirectoryInfo dir1 = new DirectoryInfo(@"C:\Users\Chocolade\AppData\Local\SatellitesImagesDownloads\SatellitesImagesDownloads\Countries\");
fi = dir1.GetFiles("*.gif", SearchOption.AllDirectories);
foreach (FileInfo finfo in fi)
{
if (fi.Length > 0 && finfo.Length > 0)
{
timer1.Enabled = false;
pictureBox1.Load(finfo.FullName);
listView1.Items[0].Checked = true;
}
}
}
您实际上已经用文字描述了您的算法:
but first only for Israel once it found Israel loaded the gif to the pictureBox1 and checked the checkbox
这里实际上不需要 for 循环,因为如果找到多个文件,您将加载最后一个文件,您可以使用 Last()
方法
DirectoryInfo dir1 = new DirectoryInfo(@"C:\Users\Chocolade\AppData\Local\SatellitesImagesDownloads\SatellitesImagesDownloads\Countries\Israel\");
fi = dir1.GetFiles("*.gif");
// if you have found any gif files at all
if (fi.Any())
{
timer1.Enabled = false;
pictureBox1.Load(fi.Last().FullName);
listView1.Items[0].Checked = true;
}
then continue and search all other sub directories and when there are gif file in a sub directory check the checkbox for it in the listView1.
fi = dir1.GetFiles("*.gif", SearchOption.AllDirectories);
// if you have found any gif files at all
if (fi.Any())
{
timer1.Enabled = false;
listView1.Items[0].Checked = true;
}
将这两个代码部分放入您的 timer1_Tick
活动中。
您也可以将它们组合成一个 if-else 语句,因为如果您在 Israel 文件夹中找到 gif 文件,您的复选框将被选中。