在做文件上传的时候经常用到判断文件的后缀名,所以我现在把代码贴在下边,代码是借用别人的,也算是拿来主义吧 bool fileok = false ; if (fileupsmpic.HasFile) ... { string fileExten = System.IO.Path.GetExtension(fileupsmpic.FileName).ToLower(); string[] AllowExten = ...{ ".gif",".bmp",".jpg" }; for (int i = 0; i < AllowExten.Length;i++ ) ...{ if (fileExten == AllowExten[i]) ...{ fileok = true; } } if(fileok) ...{ try ...{ string filePath = "../../Industry/ComWeb/"+LoginId+"/ProduPic/"; string fileName = System.DateTime.Now.ToString("yyyyMMddhhmmss")+"sm"+fileExten; fileupsmpic.PostedFile.SaveAs(ConfigurationManager.AppSettings["RootD"]+LoginId+"/ProduPic/"+fileName); } catch(System.Exception e1) ...{ //throw e1.ToString(); } } } 正则表达式比较简单: ^ (([a - zA - Z]:) | (/ ... {2} w + )$ ? )(/(w[w]. * )) + (.jpg | .Jpg | .bmp | .Bmp | .gif | .Gif)$