删除内容同时删除内容里面的图片

void delImg(string text)
    {
        string regStr = @"<img(?<Attributes1>[\s\S]*?)src=(""{1}|'{1}|)(?<picture>[^\[^>]*?(gif|jpg|jpeg|png|bmp))(""{1}|'{1}|)(?<Attributes2>[\s\S]*?)>";
        System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regStr, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        System.Text.RegularExpressions.MatchCollection mc = reg.Matches(text);
        foreach (System.Text.RegularExpressions.Match m in mc)
        {
            string filename = m.Groups["picture"].Value;
            /*
            if (filename.Length > 7 && filename.Substring(0, 7).ToLower() == "http://")
                break; //http方式有可能是网络的,这里不删除
            if (filename.Length > 5 && filename.Substring(0, 4).ToLower() == "www.")
                break;//www.开头的,也是网络的,这里不删除
            */
            if (filename.Substring(0, 1) == "/")
                filename = HttpContext.Current.Server.MapPath("~"+filename);
            else if(filename.Substring(0,1)=="~")
                filename = HttpContext.Current.Server.MapPath(filename);
            else
                filename = HttpContext.Current.Server.MapPath("~/"+filename);
 
            if (System.IO.File.Exists(filename))
            {
                System.IO.File.SetAttributes(filename, System.IO.FileAttributes.Normal);
                System.IO.File.Delete(filename);
            }
 
        }
 
    }

评论