11 Commits
UI ... 7Zip

8 changed files with 802 additions and 156 deletions

BIN
7z-x64.dll Normal file

Binary file not shown.

BIN
7z-x86.dll Normal file

Binary file not shown.

View File

@@ -11,7 +11,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-8.0.0.1" newVersion="8.0.0.1" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.6" newVersion="9.0.0.6" />
</dependentAssembly>
</assemblyBinding>
</runtime>

View File

@@ -83,17 +83,26 @@
<Reference Include="LanzouCloudSolve, Version=1.0.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\LanzouCloudSolve.1.0.3\lib\netstandard2.0\LanzouCloudSolve.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Bcl.AsyncInterfaces.8.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=9.0.0.6, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Bcl.AsyncInterfaces.9.0.6\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="SevenZipExtractor, Version=1.0.19.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\SevenZipExtractor.1.0.19\lib\net45\SevenZipExtractor.dll</HintPath>
</Reference>
<Reference Include="SevenZipSharp, Version=0.64.3890.29348, Culture=neutral, PublicKeyToken=20de82c62b055c88, processorArchitecture=MSIL">
<HintPath>packages\SevenZipSharp.0.64\lib\SevenZipSharp.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.IO.Pipelines, Version=9.0.0.6, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.IO.Pipelines.9.0.6\lib\net462\System.IO.Pipelines.dll</HintPath>
</Reference>
<Reference Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Memory.4.5.5\lib\net461\System.Memory.dll</HintPath>
</Reference>
@@ -104,11 +113,11 @@
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Text.Encodings.Web, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Text.Encodings.Web.8.0.0\lib\net462\System.Text.Encodings.Web.dll</HintPath>
<Reference Include="System.Text.Encodings.Web, Version=9.0.0.6, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Text.Encodings.Web.9.0.6\lib\net462\System.Text.Encodings.Web.dll</HintPath>
</Reference>
<Reference Include="System.Text.Json, Version=8.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Text.Json.8.0.1\lib\net462\System.Text.Json.dll</HintPath>
<Reference Include="System.Text.Json, Version=9.0.0.6, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Text.Json.9.0.6\lib\net462\System.Text.Json.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
@@ -174,6 +183,10 @@
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="7z-x86.dll" />
<EmbeddedResource Include="7z-x64.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
@@ -181,8 +194,12 @@
</PropertyGroup>
<Error Condition="!Exists('packages\Costura.Fody.6.0.0\build\Costura.Fody.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Costura.Fody.6.0.0\build\Costura.Fody.props'))" />
<Error Condition="!Exists('packages\Costura.Fody.6.0.0\build\Costura.Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Costura.Fody.6.0.0\build\Costura.Fody.targets'))" />
<Error Condition="!Exists('packages\Fody.6.8.2\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Fody.6.8.2\build\Fody.targets'))" />
<Error Condition="!Exists('packages\Fody.6.9.2\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Fody.6.9.2\build\Fody.targets'))" />
<Error Condition="!Exists('packages\SevenZipSharp.Interop.19.1.0\build\SevenZipSharp.Interop.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\SevenZipSharp.Interop.19.1.0\build\SevenZipSharp.Interop.targets'))" />
<Error Condition="!Exists('packages\SevenZipExtractor.1.0.19\build\SevenZipExtractor.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\SevenZipExtractor.1.0.19\build\SevenZipExtractor.targets'))" />
</Target>
<Import Project="packages\Costura.Fody.6.0.0\build\Costura.Fody.targets" Condition="Exists('packages\Costura.Fody.6.0.0\build\Costura.Fody.targets')" />
<Import Project="packages\Fody.6.8.2\build\Fody.targets" Condition="Exists('packages\Fody.6.8.2\build\Fody.targets')" />
<Import Project="packages\Fody.6.9.2\build\Fody.targets" Condition="Exists('packages\Fody.6.9.2\build\Fody.targets')" />
<Import Project="packages\SevenZipSharp.Interop.19.1.0\build\SevenZipSharp.Interop.targets" Condition="Exists('packages\SevenZipSharp.Interop.19.1.0\build\SevenZipSharp.Interop.targets')" />
<Import Project="packages\SevenZipExtractor.1.0.19\build\SevenZipExtractor.targets" Condition="Exists('packages\SevenZipExtractor.1.0.19\build\SevenZipExtractor.targets')" />
</Project>

41
Form1.Designer.cs generated
View File

@@ -28,20 +28,12 @@
/// </summary>
private void InitializeComponent()
{
this.Update_Text = new System.Windows.Forms.Label();
this.Update_Pro = new System.Windows.Forms.ProgressBar();
this.Status_Box = new System.Windows.Forms.Label();
this.Count_Box = new System.Windows.Forms.Label();
this.Size_Box = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// Update_Text
//
this.Update_Text.AutoSize = true;
this.Update_Text.Location = new System.Drawing.Point(12, 12);
this.Update_Text.Name = "Update_Text";
this.Update_Text.Size = new System.Drawing.Size(59, 12);
this.Update_Text.TabIndex = 0;
this.Update_Text.Text = "更新状态:";
//
// Update_Pro
//
this.Update_Pro.Location = new System.Drawing.Point(12, 36);
@@ -51,22 +43,38 @@
//
// Status_Box
//
this.Status_Box.AutoSize = true;
this.Status_Box.Location = new System.Drawing.Point(72, 12);
this.Status_Box.Location = new System.Drawing.Point(12, 12);
this.Status_Box.Name = "Status_Box";
this.Status_Box.Size = new System.Drawing.Size(23, 12);
this.Status_Box.Size = new System.Drawing.Size(148, 12);
this.Status_Box.TabIndex = 2;
this.Status_Box.Text = "...";
//
// Count_Box
//
this.Count_Box.Location = new System.Drawing.Point(296, 12);
this.Count_Box.Name = "Count_Box";
this.Count_Box.Size = new System.Drawing.Size(55, 13);
this.Count_Box.TabIndex = 3;
this.Count_Box.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// Size_Box
//
this.Size_Box.Location = new System.Drawing.Point(166, 12);
this.Size_Box.Name = "Size_Box";
this.Size_Box.Size = new System.Drawing.Size(130, 13);
this.Size_Box.TabIndex = 4;
this.Size_Box.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// Update
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(363, 73);
this.ControlBox = false;
this.Controls.Add(this.Size_Box);
this.Controls.Add(this.Count_Box);
this.Controls.Add(this.Status_Box);
this.Controls.Add(this.Update_Pro);
this.Controls.Add(this.Update_Text);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox = false;
this.MinimizeBox = false;
@@ -76,15 +84,14 @@
this.TopMost = true;
this.Load += new System.EventHandler(this.Update_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label Update_Text;
private System.Windows.Forms.ProgressBar Update_Pro;
private System.Windows.Forms.Label Status_Box;
private System.Windows.Forms.Label Count_Box;
private System.Windows.Forms.Label Size_Box;
}
}

462
Form1.cs
View File

@@ -23,6 +23,9 @@ using Newtonsoft.Json.Linq;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Configuration;
using SevenZipExtractor;
using System.Reflection;
using Microsoft.Win32;
namespace CheckDownload
{
@@ -72,12 +75,20 @@ namespace CheckDownload
// 基准目录路径(用于文件更新的目标目录)
private string _baseDirectory;
// === 新增: 用于显示整体下载大小与速度 ===
private long _totalDownloadedBytes = 0; // 已下载总字节数
private DateTime _downloadStartTime; // 下载开始时间
private DateTime _lastSpeedUpdateTime; // 上一次更新速度的时间
private readonly object _speedLock = new object(); // 锁,用于多线程更新 UI
private long _bytesSinceLastSpeedCalc = 0; // 距离上次速度计算新增的字节数
/// <summary>
/// 初始化窗体
/// </summary>
public Update()
{
InitializeComponent();
_baseDirectory = Application.StartupPath;
ConfigureProgressBar();
InitializeTempDirectory();
}
@@ -133,6 +144,38 @@ namespace CheckDownload
}
}
/// <summary>
/// 更新计数显示文本(线程安全)
/// </summary>
/// <param name="countMessage">计数值</param>
private void UpdateCount(string countMessage)
{
if (this.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate { Count_Box.Text = countMessage; });
}
else
{
Count_Box.Text = countMessage;
}
}
/// <summary>
/// 更新大小显示文本(线程安全)
/// </summary>
/// <param name="sizeMessage">大小值</param>
private void UpdateSize(string sizeMessage)
{
if (this.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate { Size_Box.Text = sizeMessage; });
}
else
{
Size_Box.Text = sizeMessage;
}
}
/// <summary>
/// 窗体加载事件处理,启动更新流程
/// </summary>
@@ -166,6 +209,8 @@ namespace CheckDownload
{
CleanupNewFiles();
UpdateStatus("下载在线MD5文件并读取...");
UpdateCount("");
UpdateSize("");
string tempFilePath = Path.Combine(_tempDirectory, Md5File);
// 使用新的带123盘的下载方法
if (!await DownloadMd5FileWithFallback(Md5File, tempFilePath))
@@ -189,9 +234,15 @@ namespace CheckDownload
UpdateStatus("比较本地和在线MD5文件...");
var compareResult = CompareMd5Data(onlineData.Data);
// 过滤掉 .db 和 .db3 文件
compareResult = compareResult
.Where(kvp => !IsDatabaseFile(kvp.Key))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
if (compareResult.Count == 0)
{
UpdateStatus("所有文件都是最新的,无需更新");
UpdateCount("");
UpdateSize("");
UpdateProgressValue(100);
// 无需更新时清理临时文件夹
@@ -204,6 +255,12 @@ namespace CheckDownload
return;
}
// 如果更新列表包含 tim.dll则提前结束 tim.exe
if (compareResult.Keys.Any(p => p.EndsWith("tim.dll", StringComparison.OrdinalIgnoreCase)))
{
KillProcessByBaseName("tim");
}
UpdateStatus("下载并验证文件...");
// 根据路径长度排序,优先下载小文件/浅层文件,可加其它排序规则
var orderedFileList = compareResult.OrderBy(k => k.Key.Length)
@@ -212,6 +269,13 @@ namespace CheckDownload
_totalCount = orderedFileList.Count;
_completedCount = 0;
_downloadedFiles.Clear();
// === 新增: 初始化速度统计 ===
_totalDownloadedBytes = 0;
_downloadStartTime = DateTime.UtcNow;
_lastSpeedUpdateTime = _downloadStartTime;
_bytesSinceLastSpeedCalc = 0;
var failedFiles = new ConcurrentDictionary<string, string>();
await PerformDownloads(orderedFileList, failedFiles);
@@ -219,10 +283,14 @@ namespace CheckDownload
if (!failedFiles.IsEmpty)
{
UpdateStatus($"有 {failedFiles.Count} 个文件下载失败,开始重试...");
UpdateCount("");
UpdateSize("");
var stillFailing = await RetryFailedFilesAsync(new Dictionary<string, string>(failedFiles));
if (stillFailing.Any())
{
UpdateStatus($"重试后仍有 {stillFailing.Count} 个文件下载失败。");
UpdateCount("");
UpdateSize("");
}
}
@@ -233,6 +301,8 @@ namespace CheckDownload
await VerifyAndSaveAllFiles();
await DecompressTim7zAsync();
// 校验和保存成功后清理临时目录
CleanupTempDirectory();
@@ -244,7 +314,8 @@ namespace CheckDownload
}
catch (Exception ex)
{
UpdateStatus($"更新失败: {ex.Message}");
UpdateStatus("更新失败");
MessageBox.Show($"更新失败:\n{ex.ToString()}", "Update Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
await Task.Delay(3000);
this.Close();
}
@@ -339,6 +410,12 @@ namespace CheckDownload
string relativePath = string.IsNullOrEmpty(currentPath) ? key : Path.Combine(currentPath, key);
string localFullPath = Path.Combine(_baseDirectory, relativePath);
// 若为数据库文件 (.db/.db3) 直接跳过比较
if (IsDatabaseFile(relativePath))
{
continue;
}
if (onlineValue.Type == JTokenType.String)
{
string expectedMd5 = onlineValue.ToString();
@@ -432,7 +509,8 @@ namespace CheckDownload
int progress = (int)((double)_completedCount / _totalCount * 95);
UpdateProgressValue(progress);
UpdateStatus($"{Path.GetFileName(file.Key)} {_completedCount}/{_totalCount}");
UpdateStatus($"{Path.GetFileName(file.Key)}");
UpdateCount($"{_completedCount}/{_totalCount}");
}
else
{
@@ -455,6 +533,12 @@ namespace CheckDownload
/// <returns>下载成功返回true否则返回false</returns>
private async Task<bool> AttemptDownloadAsync(string filePath, string expectedMd5)
{
// kill exe if running
if (Path.GetExtension(filePath).Equals(".exe", StringComparison.OrdinalIgnoreCase))
{
KillProcessIfRunning(filePath);
}
string tempFilePath = Path.Combine(_tempDirectory, filePath);
string fileName = Path.GetFileName(filePath);
@@ -465,13 +549,21 @@ namespace CheckDownload
return true;
}
UpdateStatus($"{fileName} {_completedCount + 1}/{_totalCount}");
if (!string.IsNullOrWhiteSpace(fileName))
{
UpdateStatus($"下载:{fileName}");
}
UpdateCount($"{_completedCount + 1}/{_totalCount}");
if (await DownloadFileFromOneDrive(filePath, expectedMd5, tempFilePath))
{
return true;
}
UpdateStatus($"{fileName} {_completedCount + 1}/{_totalCount}");
if (!string.IsNullOrWhiteSpace(fileName))
{
UpdateStatus($"下载:{fileName}");
}
UpdateCount($"{_completedCount + 1}/{_totalCount}");
string ossKey = $"File/{expectedMd5}";
var obj = _ossClient.GetObject(OssBucketName, ossKey);
@@ -484,19 +576,27 @@ namespace CheckDownload
using (var fileStream = File.Create(tempFilePath))
{
await obj.Content.CopyToAsync(fileStream);
await DownloadWithProgressAsync(obj.Content, fileStream, obj.Metadata.ContentLength);
}
return true;
}
catch (Exception ex) when (ex is OssException || ex is WebException)
{
UpdateStatus($"{fileName} {_completedCount + 1}/{_totalCount}");
if (!string.IsNullOrWhiteSpace(fileName))
{
UpdateStatus($"下载:{fileName}");
}
UpdateCount($"{_completedCount + 1}/{_totalCount}");
UpdateSize("");
string ossKey = $"File/{expectedMd5}";
return await DownloadFileWithFallback(ossKey, tempFilePath);
}
catch (Exception ex)
{
if (!string.IsNullOrWhiteSpace(fileName))
{
UpdateStatus($"下载异常: {fileName} - {ex.Message}");
}
return false;
}
}
@@ -517,25 +617,47 @@ namespace CheckDownload
return false;
}
if (!string.IsNullOrWhiteSpace(fileName))
{
UpdateStatus($"检查已存在的临时文件: {fileName}");
}
string actualMd5 = await Task.Run(() => CalculateMD5FromFile(tempFilePath));
if (actualMd5.Equals(expectedMd5, StringComparison.OrdinalIgnoreCase))
{
if (!string.IsNullOrWhiteSpace(fileName))
{
UpdateStatus($"临时文件完整,跳过下载: {fileName}");
}
// === 新增: 将已存在的有效临时文件大小计入总下载量 ===
try
{
var fileInfo = new FileInfo(tempFilePath);
Interlocked.Add(ref _totalDownloadedBytes, fileInfo.Length);
}
catch
{
// 忽略获取文件大小的错误
}
return true;
}
else
{
if (!string.IsNullOrWhiteSpace(fileName))
{
UpdateStatus($"临时文件不完整,重新下载: {fileName}");
}
File.Delete(tempFilePath);
return false;
}
}
catch (Exception ex)
{
if (!string.IsNullOrWhiteSpace(fileName))
{
UpdateStatus($"检查临时文件时出错,将重新下载: {fileName} - {ex.Message}");
}
try
{
if (File.Exists(tempFilePath))
@@ -562,10 +684,14 @@ namespace CheckDownload
try
{
UpdateStatus($"正在从123盘下载: {fileName}");
UpdateCount("");
UpdateSize("");
string authUrl = GenerateAuthUrl($"http://{OneDriveMainDomain}{OneDrivePath}", fileName);
UpdateStatus($"使用主域名下载文件...");
UpdateCount("");
UpdateSize("");
var request = new HttpRequestMessage(HttpMethod.Get, authUrl) { Version = HttpVersion.Version11 };
request.Headers.Add("User-Agent", "CheckDownload/1.0");
@@ -582,25 +708,33 @@ namespace CheckDownload
using (var remote = await response.Content.ReadAsStreamAsync())
using (var localFile = File.Create(localPath))
{
await remote.CopyToAsync(localFile);
await DownloadWithProgressAsync(remote, localFile, response.Content.Headers.ContentLength);
}
UpdateStatus($"123盘主域名下载成功: {fileName}");
UpdateCount("");
UpdateSize("");
return true;
}
}
catch (Exception ex)
catch (Exception)
{
UpdateStatus($"123盘主域名下载失败尝试备用域名: {fileName}");
UpdateCount("");
UpdateSize("");
}
try
{
UpdateStatus($"正在从123盘备用域名下载: {fileName}");
UpdateCount("");
UpdateSize("");
string authUrl = GenerateAuthUrl($"http://{OneDriveBackupDomain}{OneDrivePath}", fileName);
UpdateStatus($"使用备用域名下载文件...");
UpdateCount("");
UpdateSize("");
var request = new HttpRequestMessage(HttpMethod.Get, authUrl) { Version = HttpVersion.Version11 };
request.Headers.Add("User-Agent", "CheckDownload/1.0");
@@ -617,10 +751,12 @@ namespace CheckDownload
using (var remote = await response.Content.ReadAsStreamAsync())
using (var localFile = File.Create(localPath))
{
await remote.CopyToAsync(localFile);
await DownloadWithProgressAsync(remote, localFile, response.Content.Headers.ContentLength);
}
UpdateStatus($"123盘备用域名下载成功: {fileName}");
UpdateCount("");
UpdateSize("");
return true;
}
}
@@ -661,13 +797,13 @@ namespace CheckDownload
using (var remote = await response.Content.ReadAsStreamAsync())
using (var localFile = File.Create(localPath))
{
await remote.CopyToAsync(localFile);
await DownloadWithProgressAsync(remote, localFile, response.Content.Headers.ContentLength);
}
return true;
}
}
catch (Exception ex)
catch (Exception)
{
}
@@ -690,13 +826,13 @@ namespace CheckDownload
using (var remote = await response.Content.ReadAsStreamAsync())
using (var localFile = File.Create(localPath))
{
await remote.CopyToAsync(localFile);
await DownloadWithProgressAsync(remote, localFile, response.Content.Headers.ContentLength);
}
return true;
}
}
catch (Exception ex)
catch (Exception)
{
return false;
}
@@ -714,6 +850,8 @@ namespace CheckDownload
for (int i = 0; i < MaxDownloadRetries && filesToRetry.Any(); i++)
{
UpdateStatus($"第 {i + 1} 次重试,剩余 {filesToRetry.Count} 个文件...");
UpdateCount("");
UpdateSize("");
var failedThisRound = new ConcurrentDictionary<string, string>();
await PerformDownloads(filesToRetry, failedThisRound);
@@ -723,6 +861,8 @@ namespace CheckDownload
if (filesToRetry.Any() && i < MaxDownloadRetries - 1)
{
UpdateStatus($"等待 3 秒后进行下一次重试...");
UpdateCount("");
UpdateSize("");
await Task.Delay(3000);
}
}
@@ -757,7 +897,7 @@ namespace CheckDownload
return ips;
}
}
catch (Exception ex)
catch (Exception)
{
UpdateStatus($"通过 {dnsServer} 解析域名失败,尝试下一个...");
await Task.Delay(500);
@@ -774,6 +914,8 @@ namespace CheckDownload
private async Task VerifyAndSaveAllFiles()
{
UpdateStatus("正在校验文件...");
UpdateCount("");
UpdateSize("");
var failedFiles = new ConcurrentBag<string>();
var filesForScripting = new ConcurrentBag<(string original, string newFile)>();
@@ -840,6 +982,8 @@ namespace CheckDownload
else
{
UpdateStatus("所有文件校验和保存成功");
UpdateCount("");
UpdateSize("");
UpdateProgressValue(100);
}
}
@@ -1004,7 +1148,7 @@ namespace CheckDownload
}
using (var fileStream = File.Create(localPath))
{
obj.Content.CopyTo(fileStream);
await DownloadWithProgressAsync(obj.Content, fileStream, obj.Metadata.ContentLength);
}
return true;
}
@@ -1048,7 +1192,7 @@ namespace CheckDownload
using (var remote = await response.Content.ReadAsStreamAsync())
using (var localFile = File.Create(localPath))
{
await remote.CopyToAsync(localFile);
await DownloadWithProgressAsync(remote, localFile, response.Content.Headers.ContentLength);
}
}
return true;
@@ -1219,7 +1363,7 @@ namespace CheckDownload
Directory.Delete(tempPath, true);
}
}
catch (Exception ex)
catch (Exception)
{
}
}
@@ -1239,6 +1383,11 @@ namespace CheckDownload
if (!string.IsNullOrEmpty(programPathInMd5))
{
string programDirInMd5 = Path.GetDirectoryName(programPathInMd5);
if (programDirInMd5 == null)
{
programDirInMd5 = "";
}
if (string.IsNullOrEmpty(programDirInMd5))
{
programDirInMd5 = "";
@@ -1260,10 +1409,17 @@ namespace CheckDownload
_baseDirectory = Application.StartupPath;
}
}
catch (Exception ex)
catch (Exception)
{
_baseDirectory = Application.StartupPath;
}
finally
{
if (string.IsNullOrEmpty(_baseDirectory))
{
_baseDirectory = Application.StartupPath;
}
}
}
/// <summary>
@@ -1343,7 +1499,7 @@ namespace CheckDownload
return currentDir;
}
catch (Exception ex)
catch (Exception)
{
return currentDir;
}
@@ -1363,5 +1519,273 @@ namespace CheckDownload
return BitConverter.ToString(hashBytes).Replace("-", string.Empty).ToLowerInvariant();
}
}
/// <summary>
/// 异步将远程数据流写入本地文件流,并实时更新下载进度
/// </summary>
/// <param name="remoteStream">远程数据源的流</param>
/// <param name="localStream">本地文件的流</param>
/// <param name="totalBytes">文件的总字节数,用于计算进度</param>
private async Task DownloadWithProgressAsync(Stream remoteStream, Stream localStream, long? totalBytes)
{
long totalRead = 0;
byte[] buffer = new byte[8192]; // 8KB 缓冲区
int bytesRead;
while ((bytesRead = await remoteStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await localStream.WriteAsync(buffer, 0, bytesRead);
totalRead += bytesRead;
// === 新增: 统计整体下载量 ===
Interlocked.Add(ref _totalDownloadedBytes, bytesRead);
Interlocked.Add(ref _bytesSinceLastSpeedCalc, bytesRead);
// === 修改: 显示整体下载进度和速度(每 500ms 更新一次,避免频繁刷新) ===
if (DateTime.UtcNow - _lastSpeedUpdateTime > TimeSpan.FromMilliseconds(500))
{
lock (_speedLock)
{
if (DateTime.UtcNow - _lastSpeedUpdateTime > TimeSpan.FromMilliseconds(500))
{
UpdateOverallSize();
}
}
}
}
}
/// <summary>
/// 将字节大小格式化为更易读的单位B, KB, MB, GB, TB
/// </summary>
/// <param name="bytes">要格式化的字节数</param>
/// <returns>格式化后的字符串</returns>
private static string FormatBytes(long bytes)
{
string[] suffixes = { "B", "KB", "MB", "GB", "TB" };
int i = 0;
double dblSByte = bytes;
if (bytes > 1024)
{
for (i = 0; (bytes / 1024) > 0 && i < suffixes.Length - 1; i++, bytes /= 1024)
{
dblSByte = bytes / 1024.0;
}
}
return $"{dblSByte:0.0}{suffixes[i]}";
}
/// <summary>
/// 异步解压 tim.7z 文件(如果存在)。
/// </summary>
private async Task DecompressTim7zAsync()
{
try
{
var sevenZipFile = Directory.EnumerateFiles(_baseDirectory, "tim.7z", SearchOption.AllDirectories).FirstOrDefault();
if (sevenZipFile != null)
{
UpdateStatus("正在解压...");
await Task.Run(() => {
try
{
string sevenZipDllPath = Extract7zDll();
if (string.IsNullOrEmpty(sevenZipDllPath))
{
throw new Exception("无法提取7z.dll解压中止。");
}
string extractionPath = Path.GetDirectoryName(sevenZipFile);
string tempExtractionDir = Path.Combine(_tempDirectory, Path.GetFileNameWithoutExtension(sevenZipFile) + "_temp");
if (Directory.Exists(tempExtractionDir))
{
Directory.Delete(tempExtractionDir, true);
}
Directory.CreateDirectory(tempExtractionDir);
using (var archiveFile = new ArchiveFile(sevenZipFile, sevenZipDllPath))
{
archiveFile.Extract(tempExtractionDir, true);
}
bool requiresKill = Directory.EnumerateFiles(tempExtractionDir, "tim.dll", SearchOption.AllDirectories).Any();
if (requiresKill)
{
KillProcessByBaseName("tim");
}
foreach (string file in Directory.GetFiles(tempExtractionDir, "*.*", SearchOption.AllDirectories))
{
string relativePath = file.Substring(tempExtractionDir.Length + 1);
string destFile = Path.Combine(extractionPath, relativePath);
string destDir = Path.GetDirectoryName(destFile);
if (!Directory.Exists(destDir))
{
Directory.CreateDirectory(destDir);
}
File.Copy(file, destFile, true);
}
Directory.Delete(tempExtractionDir, true);
UpdateStatus("为解压的程序设置管理员权限...");
var exeFiles = Directory.GetFiles(extractionPath, "*.exe", SearchOption.AllDirectories);
foreach (var exeFile in exeFiles)
{
SetRunAsAdminCompatibility(exeFile);
}
}
catch (Exception ex)
{
throw new Exception($"解压失败: {ex.Message}");
}
});
UpdateStatus("tim.7z 解压完成。");
await Task.Delay(1000);
}
}
catch (Exception ex)
{
UpdateStatus($"处理 tim.7z 时出错: {ex.Message}");
await Task.Delay(3000);
}
}
/// <summary>
/// 为指定程序路径在注册表中设置"以管理员身份运行"的兼容性标志。
/// </summary>
/// <param name="exePath">要设置的.exe文件的完整路径。</param>
private void SetRunAsAdminCompatibility(string exePath)
{
const string keyPath = @"Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers";
try
{
using (RegistryKey key = Registry.CurrentUser.CreateSubKey(keyPath))
{
if (key != null)
{
key.SetValue(exePath, "~ RUNASADMIN");
}
else
{
UpdateStatus($"无法打开或创建注册表项: {keyPath}");
}
}
}
catch (Exception ex)
{
UpdateStatus($"设置管理员权限失败: {exePath} - {ex.Message}");
}
}
/// <summary>
/// 从嵌入的资源中提取与当前进程体系结构匹配的7z.dll到临时目录。
/// </summary>
/// <returns>提取的7z.dll的路径如果失败则返回null。</returns>
private string Extract7zDll()
{
try
{
string dllName = Environment.Is64BitProcess ? "7z-x64.dll" : "7z-x86.dll";
string resourceName = $"CheckDownload.{dllName}";
string dllPath = Path.Combine(_tempDirectory, "7z.dll");
if (File.Exists(dllPath))
{
// 可以选择在这里添加对现有DLL版本的校验但为简化我们先直接返回
return dllPath;
}
using (var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
if (resourceStream == null)
{
UpdateStatus($"在嵌入资源中未找到 {dllName}。");
return null;
}
using (var fileStream = new FileStream(dllPath, FileMode.Create, FileAccess.Write))
{
resourceStream.CopyTo(fileStream);
}
}
return dllPath;
}
catch (Exception ex)
{
UpdateStatus($"提取7z.dll失败: {ex.Message}");
return null;
}
}
// add helper to check db extension
private bool IsDatabaseFile(string relativePath)
{
string ext = Path.GetExtension(relativePath)?.ToLowerInvariant();
return ext == ".db" || ext == ".db3";
}
private void KillProcessIfRunning(string exeRelativePath)
{
try
{
string exeName = Path.GetFileNameWithoutExtension(exeRelativePath);
foreach (var proc in Process.GetProcessesByName(exeName))
{
try
{
proc.Kill();
proc.WaitForExit(5000);
}
catch { }
}
}
catch { }
}
private void KillProcessByBaseName(string baseName)
{
try
{
foreach (var proc in Process.GetProcesses())
{
if (string.Equals(proc.ProcessName, baseName, StringComparison.OrdinalIgnoreCase))
{
try
{
proc.Kill();
proc.WaitForExit(5000);
}
catch { }
}
}
}
catch { }
}
// === 新增: 更新整体下载大小与速度 ===
private void UpdateOverallSize()
{
DateTime now = DateTime.UtcNow;
double intervalSeconds = (now - _lastSpeedUpdateTime).TotalSeconds;
if (intervalSeconds <= 0) intervalSeconds = 0.1; // 防止除零
// 读取并清零自上次计算以来的字节数
long intervalBytes = Interlocked.Exchange(ref _bytesSinceLastSpeedCalc, 0);
double bytesPerSec = intervalBytes / intervalSeconds;
long downloaded = Interlocked.Read(ref _totalDownloadedBytes);
string speedText = $"{FormatBytes((long)bytesPerSec)}/s";
string sizeText = $"{FormatBytes(downloaded)}({speedText})";
_lastSpeedUpdateTime = now;
UpdateSize(sizeText);
}
}
}

195
README.md
View File

@@ -1 +1,194 @@
Check files & Download files
# CheckDownload - 智能文件更新器
这是一个C#编写的高效、智能的文件更新与下载工具。它被设计用于自动比较本地文件与服务器上的文件清单,并只下载有变动或缺失的文件,同时具备诸多高级功能以确保更新过程的稳定与顺畅。
## 核心功能
- **MD5校验更新**:通过比较本地与远程服务器上的 `md5.json` 文件,精确识别需要更新的文件,实现增量更新,节省带宽和时间。
- **多下载源支持**
- **阿里云OSS**:作为主要的稳定文件存储。
- **123网盘**作为备用下载链路通过动态生成鉴权URL进行下载。
- **智能基准目录检测**:程序能够根据 `md5.json` 中的文件结构,自动定位项目的主目录,确保即使程序被移动到子文件夹内,文件也能被更新到正确的位置。
- **并发下载与失败重试**
- 支持多线程并发下载,大幅提升下载效率。
- 内置下载失败自动重试机制,应对临时的网络波动。
- **IP直连与DNS容灾**当通过域名访问下载服务器失败时程序会自动尝试通过备用DNS服务解析IP地址并直接使用IP进行连接极大地提高了在复杂网络环境下的下载成功率。
## 高级特性与处理逻辑
- **智能解压 `tim.7z`**
- **精确进程控制**:在更新完成后,如果找到 `tim.7z` 压缩包,程序会先将其内容解压到一个临时隔离目录。
- **条件判断**:仅当在解压内容中检测到 `tim.dll` 时,程序才会去关闭正在运行的 `tim.exe` 进程,以避免文件占用。
- **安全覆盖**:完成上述检查和操作后,再将所有文件移动到最终的目标位置。这个过程避免了不必要地中断用户正在运行的程序。
- **自动设置管理员权限**:对于从 `tim.7z` 中解压出的所有 `.exe` 可执行文件程序会自动在Windows注册表中为其添加"以管理员身份运行"的兼容性设置,确保这些程序拥有足够的权限来正常工作。
- **处理文件占用**
- **自动解锁**在移动文件时如果目标文件被占用程序会先尝试等待1秒后重试。
- **下次启动时替换**:如果文件仍然被占用,程序会自动创建一个批处理脚本 (`.bat`),该脚本会在主程序退出后运行,从而在系统下次启动或空闲时完成文件的替换。
- **特定进程的自动关闭**
- 在下载任何 `.exe` 文件之前,程序会先查找并结束同名的正在运行的进程。
- 如果更新列表中包含 `tim.dll`,程序会提前结束 `tim.exe` 进程,以防止文件锁定。
- **数据库文件豁免**:程序在进行文件比较时,会自动跳过 `.db``.db3` 结尾的数据库文件,防止因这些文件频繁变动而导致的错误更新。
- **启动稳定性**:修复了在程序启动阶段可能因目录路径未初始化而导致的严重错误 (`ArgumentNullException`),确保了程序的健壮性。
## 用户界面
- **实时进度显示**:通过进度条、已完成数量/总数以及 **已下载总量(实时速度)** 的形式(两者均保留一位小数,每 0.5 秒刷新),清晰地展示更新进度。
- **简洁的状态反馈**:界面只显示当前正在处理的文件名等核心信息,避免被冗长的日志刷屏。
- **友好的错误提示**:当发生严重错误时,会弹出简明扼要的错误信息窗口,而不是难以理解的完整堆栈跟踪。
- **自动定位与退出**:窗体启动时会自动停靠在屏幕右下角,更新完成后会自动关闭,对用户干扰极小。
## 配置
程序的核心参数(如最大并发下载数、重试次数等)可以通过 `App.config` 文件进行调整。
```csharp
// App.config 示例
<appSettings>
<add key="MaxConcurrentDownloads" value="4" />
<add key="MaxDownloadRetries" value="2" />
</appSettings>
```
这个工具集成了多种故障处理和智能逻辑,旨在提供一个无人值守、高度自动化的文件更新体验。
## 🚀 主要功能
### 📥 多源下载支持
- **123盘云存储**: 支持主备域名自动切换,提供高可用性下载服务
- **阿里云OSS**: 备用下载源,确保文件下载的可靠性
- **智能DNS解析**: 使用多个DNS服务器提高域名解析成功率
### 🔄 智能更新机制
- **MD5完整性验证**: 下载前后进行MD5校验确保文件完整性
- **增量更新**: 仅下载已变更的文件,节省带宽和时间
- **断点续传**: 支持网络中断后继续下载,提高下载成功率
- **并发下载**: 可配置的多线程并发下载,显著提升下载速度
### 🛡️ 文件处理与安全
- **文件占用处理**: 智能检测并处理被占用的文件
- **批处理脚本**: 为被占用文件创建延迟替换脚本
- **临时文件管理**: 自动清理临时文件,保持系统整洁
- **路径智能识别**: 基于MD5数据自动识别项目基准目录
### 📦 自动解压功能
- **7z格式支持**: 内置7z解压引擎支持多种压缩格式
- **自动权限设置**: 解压后自动为exe文件设置管理员运行权限
- **覆盖解压**: 支持覆盖模式解压,确保文件更新
- **多架构兼容**: 自动选择32位/64位解压库适配不同运行环境
### 🔧 错误处理与重试
- **多次重试机制**: 下载失败自动重试,可配置重试次数
- **异常处理**: 完善的异常捕获和处理机制
- **状态实时显示**: 实时显示下载进度、状态和错误信息
## 🏗️ 技术架构
### 核心组件
- **.NET Framework 4.7.2**: 基于稳定的.NET Framework构建
- **异步编程**: 全面采用async/await模式确保UI响应性
- **多线程下载**: 使用SemaphoreSlim控制并发数量
- **资源嵌入**: 将依赖库嵌入程序,实现单文件部署
### 依赖库
- **Aliyun.OSS.SDK**: 阿里云对象存储服务支持
- **Newtonsoft.Json**: JSON数据处理
- **SevenZipExtractor**: 7z压缩文件解压支持
## 📋 配置说明
### 应用程序配置
程序支持通过App.config进行配置
```xml
<appSettings>
<!-- 最大并发下载数量 -->
<add key="MaxConcurrentDownloads" value="4" />
<!-- 最大下载重试次数 -->
<add key="MaxDownloadRetries" value="2" />
</appSettings>
```
### MD5文件格式
程序使用JSON格式的MD5文件来管理文件版本
```json
{
"version": "1.0.0",
"data": {
"program.exe": "5d41402abc4b2a76b9719d911017c592",
"lib/library.dll": "098f6bcd4621d373cade4e832627b4f6",
"config/settings.ini": "5e40d4c123456789abcdef1234567890"
}
}
```
## 🔄 工作流程
1. **启动检查**: 程序启动时清理旧的临时文件
2. **下载MD5**: 从云存储下载最新的MD5文件
3. **文件比较**: 对比本地文件与在线MD5识别需要更新的文件
4. **并发下载**: 使用多线程下载需要更新的文件
5. **完整性验证**: 验证下载文件的MD5值
6. **文件替换**: 将新文件移动到目标位置
7. **7z解压**: 自动检测并解压tim.7z文件
8. **权限设置**: 为解压的exe文件设置管理员运行权限
9. **清理完成**: 清理临时文件,显示完成状态
## 🎯 使用场景
- **软件自动更新**: 为桌面应用程序提供自动更新功能
- **文件同步**: 在不同设备间同步文件和配置
- **批量部署**: 企业环境下的软件批量部署和更新
- **游戏更新**: 游戏客户端的增量更新和补丁分发
## 🛠️ 开发特性
### 网络优化
- **静态HttpClient**: 避免套接字耗尽问题
- **连接池复用**: 提高网络请求效率
- **超时控制**: 合理的超时设置,避免长时间等待
### 内存管理
- **流式处理**: 大文件下载使用流式处理,控制内存占用
- **及时释放**: 及时释放不再使用的资源
- **临时文件**: 合理使用临时文件,避免内存溢出
### 用户体验
- **进度显示**: 实时显示下载进度和文件信息
- **状态反馈**: 详细的状态信息和错误提示
- **窗口定位**: 智能定位到屏幕右下角,不影响用户操作
## 📈 性能特点
- **高效下载**: 多线程并发下载,充分利用网络带宽
- **智能重试**: 失败文件智能重试,提高成功率
- **资源节约**: 仅下载变更文件,节省网络流量
- **快速启动**: 优化的启动流程,快速进入工作状态
## 🔒 安全性
- **MD5验证**: 确保文件在传输过程中没有被篡改
- **用户权限**: 在当前用户权限下操作,避免权限滥用
- **临时目录**: 使用用户临时目录,避免权限问题
- **异常处理**: 完善的异常处理,避免程序崩溃
## 📝 版本历史
### 最新版本特性
- ✅ 新增"已下载总量 + 实时下载速度"显示,速度与大小均保留一位小数,避免大文件下载时 UI 停滞误判。
- ✅ 添加7z自动解压功能
- ✅ 支持解压后程序自动设置管理员权限
- ✅ 优化多线程下载性能
- ✅ 增强错误处理和重试机制
- ✅ 改进用户界面和状态显示
- ✅ 过滤 .db / .db3 数据库文件,避免占用导致失败
- ✅ 更新列表或压缩包中出现 tim.dll 时,自动结束 tim.exe 后替换
- ✅ 所有文件匹配与进程检测均采用大小写不敏感逻辑,避免大小写差异造成的更新失败
- ✅ 精简 Status_Box 文本,并在失败时弹窗展示核心错误信息
## 🤝 技术支持
如需技术支持或报告问题,请联系开发团队。
---
**注意**: 本程序需要网络连接以下载更新文件。首次运行时可能需要较长时间来下载必要的文件。

View File

@@ -1,18 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="7z" version="15.8.0" targetFramework="net472" />
<package id="Aliyun.OSS.SDK" version="2.14.1" targetFramework="net472" />
<package id="Costura.Fody" version="6.0.0" targetFramework="net472" developmentDependency="true" />
<package id="DnsClient" version="1.8.0" targetFramework="net472" />
<package id="Fody" version="6.8.2" targetFramework="net472" developmentDependency="true" />
<package id="Fody" version="6.9.2" targetFramework="net472" developmentDependency="true" />
<package id="LanzouCloudSolve" version="1.0.3" targetFramework="net472" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="8.0.0" targetFramework="net472" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="9.0.6" targetFramework="net472" />
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net472" />
<package id="SevenZipExtractor" version="1.0.19" targetFramework="net472" />
<package id="SevenZipSharp" version="0.64" targetFramework="net472" />
<package id="SevenZipSharp.Interop" version="19.1.0" targetFramework="net472" />
<package id="System.Buffers" version="4.5.1" targetFramework="net472" />
<package id="System.IO.Pipelines" version="9.0.6" targetFramework="net472" />
<package id="System.Memory" version="4.5.5" targetFramework="net472" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net472" />
<package id="System.Text.Encodings.Web" version="8.0.0" targetFramework="net472" />
<package id="System.Text.Json" version="8.0.1" targetFramework="net472" />
<package id="System.Text.Encodings.Web" version="9.0.6" targetFramework="net472" />
<package id="System.Text.Json" version="9.0.6" targetFramework="net472" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net472" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net472" />
</packages>