mirror of
https://github.com/huiyadanli/RevokeMsgPatcher.git
synced 2025-05-24 14:36:06 +08:00
捕获错误。修复找不到微信注册表路径时,程序闪退的问题
This commit is contained in:
parent
22b8138283
commit
43ce6ecb96
|
@ -34,7 +34,9 @@
|
||||||
|
|
||||||
## :book:使用方法
|
## :book:使用方法
|
||||||
|
|
||||||
**请以管理员身份运行本程序,由于修改了微信的 `WeChatWin.dll` 文件,杀毒软件可能会弹出警告,放行即可**
|
**请以管理员身份运行本程序,由于修改了微信的 `WeChatWin.dll` 文件,杀毒软件可能会弹出警告,放行即可。**
|
||||||
|
|
||||||
|
启动时会自动从注册表中获取微信的安装路径,如果没找到,需要手动选择微信路径。
|
||||||
|
|
||||||
环境要求:
|
环境要求:
|
||||||
|
|
||||||
|
|
|
@ -14,45 +14,62 @@ namespace RevokeMsgPatcher
|
||||||
[STAThread]
|
[STAThread]
|
||||||
static void Main()
|
static void Main()
|
||||||
{
|
{
|
||||||
Application.EnableVisualStyles();
|
try
|
||||||
Application.SetCompatibleTextRenderingDefault(false);
|
{
|
||||||
|
Application.EnableVisualStyles();
|
||||||
|
Application.SetCompatibleTextRenderingDefault(false);
|
||||||
|
|
||||||
#if DEBUG
|
#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());
|
Application.Run(new FormMain());
|
||||||
}
|
#else
|
||||||
else
|
//当前用户是管理员的时候,直接启动应用程序
|
||||||
{
|
//如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
|
||||||
//创建启动对象
|
//获得当前登录的Windows用户标示
|
||||||
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
|
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
|
||||||
startInfo.UseShellExecute = true;
|
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
|
||||||
startInfo.WorkingDirectory = Environment.CurrentDirectory;
|
//判断当前登录用户是否为管理员
|
||||||
startInfo.FileName = Application.ExecutablePath;
|
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
|
||||||
//设置启动动作,确保以管理员身份运行
|
|
||||||
startInfo.Verb = "runas";
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
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
|
#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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,25 +17,32 @@ namespace RevokeMsgPatcher
|
||||||
public static string AutoFindInstallPath()
|
public static string AutoFindInstallPath()
|
||||||
{
|
{
|
||||||
// 微信的注册表路径
|
// 微信的注册表路径
|
||||||
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\WeChat");
|
try
|
||||||
object installLocation = key.GetValue("InstallLocation");
|
|
||||||
key.Close();
|
|
||||||
if (installLocation == null || string.IsNullOrEmpty(installLocation.ToString()) || !IsWechatInstallPath(installLocation.ToString()))
|
|
||||||
{
|
{
|
||||||
// 从默认安装目录查找
|
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall\WeChat");
|
||||||
string[] drives = Environment.GetLogicalDrives(); //获取当前计算机逻辑磁盘名称列表
|
object installLocation = key.GetValue("InstallLocation");
|
||||||
foreach (string d in drives)
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user