Explorar el Código

读取注册表自动识别微信位置

XMeng hace 1 año
padre
commit
22234ebe08

+ 3 - 2
WechatDoubleOpen/Program.cs

@@ -11,9 +11,10 @@ namespace WechatDoubleOpen
     {
         static void Main(string[] args)
         {
+            string WechatPath = ReadWeChatPathClass.ReadWeChatPath();
             //打开两个微信
-            Process.Start("E:\\WeChat\\WeChat.exe");
-            Process.Start("E:\\WeChat\\WeChat.exe");
+            Process.Start(WechatPath);
+            Process.Start(WechatPath);
         }
     }
 }

+ 29 - 0
WechatDoubleOpen/ReadWeChatPathClass.cs

@@ -0,0 +1,29 @@
+using Microsoft.Win32;
+using System;
+using System.Security.AccessControl;
+
+public static class ReadWeChatPathClass
+{
+    public static string ReadWeChatPath()
+    {
+        // 定义要读取的注册表路径
+        string registryPath = @"SOFTWARE\Tencent\WeChat";
+        string result = "";
+        // 打开注册表项
+        using (RegistryKey key = Registry.CurrentUser.OpenSubKey(registryPath,
+                                 RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.FullControl))
+        { 
+            if (key != null)
+            {
+                // 读取指定的键值
+                object value = key.GetValue("InstallPath");
+
+                if (value != null)
+                {
+                    result = value.ToString();
+                }
+            }
+        }
+        return result + "\\WeChat.exe";
+    }
+}

+ 5 - 1
WechatDoubleOpen/WechatDoubleOpen.csproj

@@ -5,7 +5,7 @@
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
     <ProjectGuid>{67C0BB5E-B5CC-49DA-8605-AE5B86512C4F}</ProjectGuid>
-    <OutputType>Exe</OutputType>
+    <OutputType>WinExe</OutputType>
     <RootNamespace>WechatDoubleOpen</RootNamespace>
     <AssemblyName>WechatDoubleOpen</AssemblyName>
     <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
@@ -32,6 +32,9 @@
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
+  <PropertyGroup>
+    <StartupObject />
+  </PropertyGroup>
   <ItemGroup>
     <Reference Include="System" />
     <Reference Include="System.Core" />
@@ -45,6 +48,7 @@
   <ItemGroup>
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="ReadWeChatPathClass.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="App.config" />