1 stringstr ="<form id=\ "form1\" runat=\ "Server\" ><div><p></p><p></p><p></p></div></form> ";2 for(intj =0; J < Gethtmlimageurllist (str). Length; J + +)3 {4 //get Picture path string5 stringIMG =gethtmlimageurllist (str) [i];6 //Delete a picture on a server7 stringfilepath =Server.MapPath (IMG);8FileInfo file =NewFileInfo (filepath);9 if(file. Exists)Ten { One file. Delete (); A } -}
/// <summary> ///get the path to IMG/// </summary> /// <param name= "HTMLText" >HTML string Literals</param> /// <returns>return the picture path as an array</returns> Public Static string[] Gethtmlimageurllist (stringhtmltext) {Regex regimg=NewRegex (@"]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*["'"? [\s\t\r\n]* (? [^\s\t\r\n "" ' <>]* ") [^<>]*?/? [\s\t\r\n]*>", regexoptions.ignorecase); //create a new matches MatchCollection object to save the number of matching objects (img tag)MatchCollection matches =regimg.matches (HTMLText); inti =0; string[] Surllist =New string[matches. Count]; //iterate through all IMG Tag Objects foreach(Match matchinchmatches) { //gets the path of all IMG SRC, and saves it to the arraysurllist[i++] = match. groups["Imgurl"]. Value; } returnsurllist; }
C # regular Get Img src path