使用C#读取网站相对路径文件夹下所有图片
public JsonResult GetCourseInitCover() { //设置相对路径 string imgurl = Server.MapPath("~/Content/images"); //根据相对路径,读取所有文件,并且不查找子文件夹。之后筛选格式是图片的文件 var strs = System.IO.Directory.GetFiles(imgurl, "*.*", System.IO.SearchOption.TopDirectoryOnly).Where(s => s.EndsWith(".jpg") || s.EndsWith(".gif") || s.EndsWith(".bmp") || s.EndsWith(".png")); List<KeyName> filelist = new List<KeyName>(); foreach (string file in strs) { KeyName item = new KeyName(); //读取文件信息 System.IO.FileInfo fi = new System.IO.FileInfo(file); if (fi.Extension == ".jpg" || fi.Extension == ".gif" || fi.Extension == ".bmp" || fi.Extension == ".png") { item.Name = fi.Name; filelist.Add(item); } } //拼装返回参数 var result = from dir in filelist select new { key = "/Content/images/" + dir.Name, value = dir.Name.Split('.')[0] }; return Json(result, JsonRequestBehavior.AllowGet); }