From 4ebf1149a460e1eb3ffc31782e5c31397a3ef6ab Mon Sep 17 00:00:00 2001 From: dong <1278815766@qq.com> Date: Sun, 29 Jun 2025 21:42:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AE=A1=E7=90=86=E5=91=98?= =?UTF-8?q?=E6=9D=83=E9=99=90=E8=AE=BE=E7=BD=AE=E5=8A=9F=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=A7=A3=E5=8E=8B=E7=8A=B6=E6=80=81=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=EF=BC=8C=E5=B9=B6=E4=BC=98=E5=8C=96=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Form1.cs | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/Form1.cs b/Form1.cs index 4024e1a..5f7f102 100644 --- a/Form1.cs +++ b/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); } } + /// + /// 为指定程序路径在注册表中设置"以管理员身份运行"的兼容性标志。 + /// + /// 要设置的.exe文件的完整路径。 + 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}"); + } + } + /// /// 从嵌入的资源中提取与当前进程体系结构匹配的7z.dll到临时目录。 ///