diff --git a/README.md b/README.md index 98786d5..1dca584 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,9 @@ ## :book:使用方法 -**请以管理员身份运行本程序,由于修改了微信的 `WeChatWin.dll` 文件,杀毒软件可能会弹出警告,放行即可** +**请以管理员身份运行本程序,由于修改了微信的 `WeChatWin.dll` 文件,杀毒软件可能会弹出警告,放行即可。** + +启动时会自动从注册表中获取微信的安装路径,如果没找到,需要手动选择微信路径。 环境要求: diff --git a/RevokeMsgPatcher/Program.cs b/RevokeMsgPatcher/Program.cs index 2d5b549..7c19d17 100644 --- a/RevokeMsgPatcher/Program.cs +++ b/RevokeMsgPatcher/Program.cs @@ -14,45 +14,62 @@ namespace RevokeMsgPatcher [STAThread] static void Main() { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); + try + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); #if DEBUG - Application.Run(new FormMain()); -#else - //当前用户是管理员的时候,直接启动应用程序 - //如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行 - //获得当前登录的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 +#else + //当前用户是管理员的时候,直接启动应用程序 + //如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行 + //获得当前登录的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)) { - System.Diagnostics.Process.Start(startInfo); + //如果是管理员,则直接运行 + Application.Run(new FormMain()); } - catch + else { - return; + //创建启动对象 + 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(); } - //退出 - Application.Exit(); - } - #endif + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + + static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) + { + MessageBox.Show(e.Exception.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + MessageBox.Show((e.ExceptionObject as Exception).Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } diff --git a/RevokeMsgPatcher/Util.cs b/RevokeMsgPatcher/Util.cs index 32e8668..3b40939 100644 --- a/RevokeMsgPatcher/Util.cs +++ b/RevokeMsgPatcher/Util.cs @@ -17,25 +17,32 @@ namespace RevokeMsgPatcher public static string AutoFindInstallPath() { // 微信的注册表路径 - RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\WeChat"); - object installLocation = key.GetValue("InstallLocation"); - key.Close(); - if (installLocation == null || string.IsNullOrEmpty(installLocation.ToString()) || !IsWechatInstallPath(installLocation.ToString())) + try { - // 从默认安装目录查找 - string[] drives = Environment.GetLogicalDrives(); //获取当前计算机逻辑磁盘名称列表 - foreach (string d in drives) + RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\WeChat"); + object installLocation = key.GetValue("InstallLocation"); + key.Close(); + if (installLocation == null || string.IsNullOrEmpty(installLocation.ToString()) || !IsWechatInstallPath(installLocation.ToString())) { - string assertPath = Path.Combine(d, @"Program Files (x86)\Tencent\WeChat"); - if (IsWechatInstallPath(assertPath)) + // 从默认安装目录查找 + string[] drives = Environment.GetLogicalDrives(); //获取当前计算机逻辑磁盘名称列表 + foreach (string d in drives) { - return assertPath; + string assertPath = Path.Combine(d, @"Program Files (x86)\Tencent\WeChat"); + if (IsWechatInstallPath(assertPath)) + { + return assertPath; + } } } + else + { + return installLocation.ToString(); + } } - else + catch(Exception e) { - return installLocation.ToString(); + Console.WriteLine(e.Message); } return null; }