C# 如何在WebBrowser控件C中检查Html元素是否具有背景图像css属性

C# 如何在WebBrowser控件C中检查Html元素是否具有背景图像css属性,c#,css,styles,webbrowser-control,html-manipulation,C#,Css,Styles,Webbrowser Control,Html Manipulation,这是我的密码: 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 propert

这是我的密码:

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
                }
            }
        }

我搜索了很多,但都没有结果:

我写了自己的扩展方法:

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;
    }
}
现在我可以调用我的方法:

var allEles = webBrowser1.Document.All;
    foreach (HtmlElement item in allEles)
    {
        if (item.TagName.ToLower() == "div")
        {
            if(item.hasBackgroundImage("myCssFolderPathHere"))
            {
                //do anything
            }
        }
    }
但只有在运行本地html文件时,此功能才起作用。。。。因为您必须在我的扩展方法中将css文件夹路径作为参数抛出,而这正是我想要的: