添加管理员权限设置功能,更新解压状态信息,并优化异常处理
This commit is contained in:
38
Form1.cs
38
Form1.cs
@@ -25,6 +25,7 @@ using System.Diagnostics;
|
||||
using System.Configuration;
|
||||
using SevenZipExtractor;
|
||||
using System.Reflection;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace CheckDownload
|
||||
{
|
||||
@@ -1490,7 +1491,7 @@ namespace CheckDownload
|
||||
|
||||
if (sevenZipFile != null)
|
||||
{
|
||||
UpdateStatus("发现tim.7z,正在解压...");
|
||||
UpdateStatus("正在解压...");
|
||||
|
||||
await Task.Run(() => {
|
||||
try
|
||||
@@ -1506,6 +1507,13 @@ namespace CheckDownload
|
||||
{
|
||||
archiveFile.Extract(extractionPath, true);
|
||||
}
|
||||
|
||||
UpdateStatus("为解压的程序设置管理员权限...");
|
||||
var exeFiles = Directory.GetFiles(extractionPath, "*.exe", SearchOption.AllDirectories);
|
||||
foreach (var exeFile in exeFiles)
|
||||
{
|
||||
SetRunAsAdminCompatibility(exeFile);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -1520,11 +1528,37 @@ namespace CheckDownload
|
||||
catch (Exception ex)
|
||||
{
|
||||
UpdateStatus($"处理 tim.7z 时出错: {ex.Message}");
|
||||
MessageBox.Show($"处理 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>
|
||||
|
Reference in New Issue
Block a user