2014-10-22 4 views

답변

0

난 내 자신의 확장을 썼다에 많이 검색

var allEles = webBrowser1.Document.All; 
     foreach (HtmlElement item in allEles) 
     { 
      if (item.TagName.ToLower() == "div") 
      { 
       if(//Here i want to check if div has a background-image css property) 
       { 
        //do anything 
       } 
      } 
     } 

방법 :

var allEles = webBrowser1.Document.All; 
    foreach (HtmlElement item in allEles) 
    { 
     if (item.TagName.ToLower() == "div") 
     { 
      if(item.hasBackgroundImage("myCssFolderPathHere")) 
      { 
       //do anything 
      } 
     } 
    } 
:

public static class Extensions 
{ 
    public static bool hasBackgroundImage(this HtmlElement ele, string cssFolderPath) 
    { 
     string styleAttr = ele.Style.ToLower(); 
     if (styleAttr.IndexOf("background-image") != -1 || styleAttr.IndexOf("background") != -1) 
     { 
      if (styleAttr.IndexOf("url") != -1) 
      { 
       return true; 
      } 
     } 
     string[] classes = ele.GetAttribute("className").Split(' '); 
     foreach (string className in classes) 
     { 
      if (className.Trim() == "") 
      { 
       continue; 
      } 
      System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(cssFolderPath); 
      foreach (System.IO.FileInfo item in d.GetFiles().Where(p => p.Extension == ".css")) 
      { 
       string cssFile = System.IO.File.ReadAllText(item.FullName); 
       int start = cssFile.IndexOf(className); 
       if (start != -1) 
       { 
        string sub = cssFile.Substring(start + className.Length); 
        int end = sub.IndexOf('}'); 
        string cssProps = sub.Substring(1, end).Replace("{", "").Replace("}", "").ToLower(); 
        if (cssProps.IndexOf("background-image") != -1 || cssProps.IndexOf("background") != -1) 
        { 
         if (cssProps.IndexOf("url") != -1) 
         { 
          return true; 
         } 
        } 
       } 
      } 
     } 
     return false; 
    } 
} 

지금 난 내 메서드를 호출 할 수 있습니다

그러나 로컬 HTML 파일을 실행하고있는 경우에만이 작업이 가능합니다. 내 확장 메서드에서 매개 변수로 CSS 폴더 경로를 던져야하고, 내가 찾던 내용이 필요합니다.