c#遍历指定目录下的所有图片
指定文件夹是up 要遍历所有图片并取得图片路径放到string[]或者list里.帮写个方法...
指定文件夹是 up 要遍历所有图片 并 取得图片路径放到string[]或者list里.帮写个方法
展开
3个回答
展开全部
我亲自在本机测试过了,代码如下:
string[] list = this.recursed(@"D:\Downloads\", new string[] { "*.jpg", "*.png", "*.gif" });
foreach (string str in list) {
this.txtProcessInfo.Text += str + Environment.NewLine;
}
public string[] recursed(string path, string[] patterns) {
string[] arrList = new string[0];
foreach (string str in patterns) {
string[] list = Directory.GetFiles(path, str, SearchOption.AllDirectories);
if (list != null) {
string[] temp = arrList;
arrList = new string[arrList.Length + list.Length];
Array.Copy(temp, 0, arrList, 0, temp.Length);
Array.Copy(list, 0, arrList, temp.Length, list.Length);
}
}
return arrList;
}
string[] list = this.recursed(@"D:\Downloads\", new string[] { "*.jpg", "*.png", "*.gif" });
foreach (string str in list) {
this.txtProcessInfo.Text += str + Environment.NewLine;
}
public string[] recursed(string path, string[] patterns) {
string[] arrList = new string[0];
foreach (string str in patterns) {
string[] list = Directory.GetFiles(path, str, SearchOption.AllDirectories);
if (list != null) {
string[] temp = arrList;
arrList = new string[arrList.Length + list.Length];
Array.Copy(temp, 0, arrList, 0, temp.Length);
Array.Copy(list, 0, arrList, temp.Length, list.Length);
}
}
return arrList;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询