From 2ffc540d87f8727a73aa86fcd1d835c5b682c6ed Mon Sep 17 00:00:00 2001 From: huiyadanli Date: Tue, 23 Jul 2019 22:58:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=90=AF=E5=8A=A8=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=EF=BC=9A=E9=9D=9E=E7=AE=A1=E7=90=86=E5=91=98=E6=9D=83?= =?UTF-8?q?=E9=99=90=E5=90=AF=E5=8A=A8=E4=BC=9A=E5=BC=B9=E5=87=BAUAC?= =?UTF-8?q?=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RevokeMsgPatcher/FormMain.cs | 2 +- RevokeMsgPatcher/Program.cs | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/RevokeMsgPatcher/FormMain.cs b/RevokeMsgPatcher/FormMain.cs index c8c7581..012c1c3 100644 --- a/RevokeMsgPatcher/FormMain.cs +++ b/RevokeMsgPatcher/FormMain.cs @@ -67,7 +67,7 @@ namespace RevokeMsgPatcher catch (Exception ex) { Console.WriteLine(ex.Message); - MessageBox.Show(ex.Message); + MessageBox.Show(ex.Message + " 请以管理员权限启动本程序,并确认微信处于关闭状态。"); } btnPatch.Enabled = true; } diff --git a/RevokeMsgPatcher/Program.cs b/RevokeMsgPatcher/Program.cs index 6141c46..695aaad 100644 --- a/RevokeMsgPatcher/Program.cs +++ b/RevokeMsgPatcher/Program.cs @@ -16,7 +16,41 @@ namespace RevokeMsgPatcher { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new FormMain()); + + //Application.Run(new FormMain()); + //return; + + //当前用户是管理员的时候,直接启动应用程序 + //如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行 + //获得当前登录的Windows用户标示 + System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); + System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); + //判断当前登录用户是否为管理员 + if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) + { + //如果是管理员,则直接运行 + Application.Run(new FormMain()); + } + else + { + //创建启动对象 + System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); + startInfo.UseShellExecute = true; + startInfo.WorkingDirectory = Environment.CurrentDirectory; + startInfo.FileName = Application.ExecutablePath; + //设置启动动作,确保以管理员身份运行 + startInfo.Verb = "runas"; + try + { + System.Diagnostics.Process.Start(startInfo); + } + catch + { + return; + } + //退出 + Application.Exit(); + } } } }