FormMultiInstance.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace RevokeMsgPatcher.MultiInstance
  14. {
  15. public partial class FormMultiInstance : Form
  16. {
  17. public FormMultiInstance()
  18. {
  19. InitializeComponent();
  20. string installFolder = FindInstallPathFromRegistry("Wechat");
  21. if (!string.IsNullOrEmpty(installFolder))
  22. {
  23. string wechatPath = Path.Combine(installFolder, "WeChat.exe");
  24. if (File.Exists(wechatPath))
  25. {
  26. txtPath.Text = wechatPath;
  27. }
  28. }
  29. }
  30. private void btnChoosePath_Click(object sender, EventArgs e)
  31. {
  32. OpenFileDialog dialog = new OpenFileDialog
  33. {
  34. Multiselect = false,
  35. Title = "请选择微信启动主程序",
  36. Filter = "微信主程序|WeChat.exe"
  37. };
  38. if (dialog.ShowDialog() == DialogResult.OK)
  39. {
  40. txtPath.Text = dialog.FileName;
  41. }
  42. }
  43. private void btnStart_Click(object sender, EventArgs e)
  44. {
  45. if (File.Exists(txtPath.Text))
  46. {
  47. // 检测微信进程是否存在
  48. Process[] ps = Process.GetProcessesByName("WeChat");
  49. if (ps.Length > 0)
  50. {
  51. DialogResult result = MessageBox.Show("当前存在运行中的微信进程,请先关闭当前微信才能使用该功能。点击【确定】强制关闭当前所有微信进程并进行多开,点击【取消】不做任何处理。", "当前存在运行中的微信进程", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
  52. if (result == DialogResult.OK)
  53. {
  54. foreach (Process p in ps)
  55. p.Kill();
  56. }
  57. else
  58. {
  59. return;
  60. }
  61. }
  62. // 启动多个实例
  63. for (int i = 0; i < startNum.Value; i++)
  64. {
  65. Process.Start(txtPath.Text);
  66. }
  67. }
  68. }
  69. private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  70. {
  71. Process.Start("https://github.com/huiyadanli/RevokeMsgPatcher");
  72. }
  73. /// <summary>
  74. /// 从注册表中寻找安装路径
  75. /// </summary>
  76. /// <param name="uninstallKeyName">
  77. /// 安装信息的注册表键名
  78. /// 微信:WeChat
  79. /// QQ:{052CFB79-9D62-42E3-8A15-DE66C2C97C3E}
  80. /// TIM:TIM
  81. /// </param>
  82. /// <returns>安装路径</returns>
  83. public static string FindInstallPathFromRegistry(string uninstallKeyName)
  84. {
  85. try
  86. {
  87. RegistryKey key = Registry.LocalMachine.OpenSubKey($@"Software\Microsoft\Windows\CurrentVersion\Uninstall\{uninstallKeyName}");
  88. if (key == null)
  89. {
  90. return null;
  91. }
  92. object installLocation = key.GetValue("InstallLocation");
  93. key.Close();
  94. if (installLocation != null && !string.IsNullOrEmpty(installLocation.ToString()))
  95. {
  96. return installLocation.ToString();
  97. }
  98. }
  99. catch (Exception e)
  100. {
  101. Console.WriteLine(e.Message);
  102. }
  103. return null;
  104. }
  105. }
  106. }