How can I get a web site's favicon?

前端 未结 14 2142
旧巷少年郎
旧巷少年郎 2020-12-22 15:28

Simple enough question: I\'ve created a small app that is basically just a favourites that sits in my system tray so that I can open often-used sites/folders/files from the

14条回答
  •  失恋的感觉
    2020-12-22 15:39

    The first thing to look for is /favicon.ico in the site root; something like WebClient.DownloadFile() should do fine. However, you can also set the icon in metadata - for SO this is:

    
    

    and note that alternative icons might be available; the "touch" one tends to be bigger and higher res, for example:

    
    

    so you would parse that in either the HTML Agility Pack or XmlDocument (if xhtml) and use WebClient.DownloadFile()

    Here's some code I've used to obtain this via the agility pack:

    var favicon = "/favicon.ico";
    var el=root.SelectSingleNode("/html/head/link[@rel='shortcut icon' and @href]");
    if (el != null) favicon = el.Attributes["href"].Value;
    

    Note the icon is theirs, not yours.

提交回复
热议问题