62,041
社区成员
发帖
与我相关
我的任务
分享
void delImg(string text)
{
string regStr = @"<img(?<Attributes1>[\s\S]*?)src=(""{1}|'{1}|)(?<picture>[^\[^>]*?(gif|jpg|jpeg|bmp|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 = Server.MapPath("~"+filename);
else if(filename.Substring(0,1)=="~")
filename = Server.MapPath(filename);
else
filename = Server.MapPath("~/"+filename);
if (System.IO.File.Exists(filename))
{
System.IO.File.SetAttributes(filename, System.IO.FileAttributes.Normal);
System.IO.File.Delete(filename);
}
}
}