方法1:
var OK_Img = Directory.GetFiles(PROJECT_DIR_OK, "*.*",
SearchOption.TopDirectoryOnly).Where(s => s.EndsWith(".tif") || s.EndsWith(".png") || s.EndsWith(".jpeg") || s.EndsWith(".TIF") || s.EndsWith(".jpg") || s.EndsWith(".bmp") || s.EndsWith(".JPG"));
if (listBox1.Items.Count != 0) listBox1.Items.Clear();
foreach (string ok_Img_name in OK_Img)
{
listBox1.Items.Add(Path.GetFileName(ok_Img_name));
}
读取 PROJECT_DIR_OK 路径下对应后缀名的图像路径,更新 listBox1 列表。
方法2:
private string[] GetImages(string dirPath, params string[] searchPatterns) //选后缀名文件
{
if (searchPatterns.Length <= 0)
{
return null;
}
else
{
DirectoryInfo di = new DirectoryInfo(dirPath);
FileInfo[][] fis = new FileInfo[searchPatterns.Length][];
int count = 0;
for (int i = 0; i < searchPatterns.Length; i++)
{
FileInfo[] fileInfos = di.GetFiles(searchPatterns[i]);
fis[i] = fileInfos;
count += fileInfos.Length;
}
string[] files = new string[count];
int n = 0;
for (int i = 0; i <= fis.GetUpperBound(0); i++)
{
for (int j = 0; j < fis[i].Length; j++)
{
string temp = fis[i][j].FullName;
files[n] = temp;
n++;
}
}
return files;
}
}
使用方法:
string[] path_OpenImg = GetImages(path_OpenFile, "*.tif", "*.jpg", "*.jpeg", "*.png", "*.bmp", "JPG");
返回对应后缀名称图像的路径。