From 800b5dc4db2b14ee9c0c9aea2e87adc9c49ad87a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8C=BF=E4=BA=BA=E6=98=93?= Date: Fri, 6 Dec 2024 22:12:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=96=87=E4=BB=B6=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E9=80=BB=E8=BE=91=E5=B9=B6=E4=BF=AE=E6=94=B9=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=AE=B0=E5=BD=95=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 删除了在下载文件之前检查并删除已存在文件的代码块,并将其移到下载文件之后。 在下载文件之后,添加了检查并删除已存在文件的代码块。 修改了日志记录中获取文件长度的方式,从 `saveFile.Length` 改为 `savePath.AsFile().Length`。 --- Pek.AspNetCore/Helpers/DHWeb.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Pek.AspNetCore/Helpers/DHWeb.cs b/Pek.AspNetCore/Helpers/DHWeb.cs index 9609a96..5ac1139 100644 --- a/Pek.AspNetCore/Helpers/DHWeb.cs +++ b/Pek.AspNetCore/Helpers/DHWeb.cs @@ -827,12 +827,6 @@ public static async Task DownloadLinkAndExtract(String url, String name, String var savePath = destdir.CombinePath(name); // 替换为你想保存的路径 savePath.EnsureDirectory(); - var saveFile = savePath.AsFile(); - if (saveFile.Exists) - { - saveFile.Delete(); - } - var sw = Stopwatch.StartNew(); var responseData = await Client().Get(UrlHelper.Combine(url, name)).DownloadDataAsync().ConfigureAwait(false); sw.Stop(); @@ -843,9 +837,15 @@ public static async Task DownloadLinkAndExtract(String url, String name, String return; } - FileUtil.Write(savePath, responseData!); // 保存文件 + var saveFile = savePath.AsFile(); + if (saveFile.Exists) + { + saveFile.Delete(); + } + + FileUtil.Write(savePath, responseData); // 保存文件 - XTrace.Log.Info("下载完成,共{0:n0}字节,耗时{1:n0}毫秒", saveFile.Length, sw.ElapsedMilliseconds); + XTrace.Log.Info("下载完成,共{0:n0}字节,耗时{1:n0}毫秒", savePath.AsFile().Length, sw.ElapsedMilliseconds); savePath.AsFile().Extract(destdir, overwrite);