跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

浏览文件

FaviconDownloader.cs

50 行 · 1.5 KB历史下载
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Net;
6using System.Text;
7using System.Threading.Tasks;
8
9namespace Core.Librarys.Browser.Favicon
10{
11 public class FaviconDownloader
12 {
13 /// <summary>
14 /// 下载文件
15 /// </summary>
16 /// <param name="url_">文件远程地址</param>
17 /// <param name="savePath_">本地保存路径</param>
18 public static async Task<string> DownloadAsync(string url_, string saveName_)
19 {
20 if (string.IsNullOrEmpty(url_)) return string.Empty;
21
22 string savePath = Path.Combine(FileHelper.GetRootDirectory(), "WebFavicons", saveName_);
23 string dir = Path.GetDirectoryName(savePath);
24 // 暂时不支持svg格式ico,尝试获取png
25 url_ = url_.Replace(".svg", ".png");
26
27 if (!Directory.Exists(dir))
28 {
29 Directory.CreateDirectory(dir);
30 }
31 if (File.Exists(savePath))
32 {
33 return savePath;
34 }
35 try
36 {
37 using (var web = new WebClient())
38 {
39 await web.DownloadFileTaskAsync(url_, savePath);
40 return savePath;
41 }
42 }
43 catch (Exception e)
44 {
45 Logger.Error("下载图标失败," + e);
46 return string.Empty;
47 }
48 }
49 }
50}