csharp: Getting all image files in folder
1 /// <summary> 2 /// 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 protected void Page_Load(object sender, EventArgs e) 7 { 8 if (!IsPostBack) 9 { 10 path = Server.MapPath("images/"); 11 12 String searchFolder = path; 13 String[] filters = new String[] { "jpg", "jpeg", "png", "gif", "tiff", "bmp" }; 14 String[] files = GetFilesFrom(searchFolder, filters, false); 15 for (int i = 0; i < files.Length; i++) 16 { 17 Response.Write(files[i].ToString()+"<br/>"); 18 } 19 20 21 } 22 23 24 } 25 /// <summary> 26 /// 查找文件夹下的图片 27 /// 2014-08-28 28 /// 涂聚文 29 /// </summary> 30 /// <param name="searchFolder"></param> 31 /// <param name="filters"></param> 32 /// <param name="isRecursive"></param> 33 /// <returns></returns> 34 public static String[] GetFilesFrom(String searchFolder, String[] filters, bool isRecursive) 35 { 36 List<String> filesFound = new List<String>(); 37 SearchOption searchOption = isRecursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; 38 foreach (string filter in filters) 39 { 40 filesFound.AddRange(Directory.GetFiles(searchFolder, String.Format("*.{0}", filter), searchOption)); 41 } 42 return filesFound.ToArray(); 43 }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)