From 4b99a5c2100b93e692ddbe8dc183b4adffb3f547 Mon Sep 17 00:00:00 2001 From: Syrone Wong Date: Thu, 20 Oct 2016 08:05:25 +0800 Subject: [PATCH] Make registry opening more generic Signed-off-by: Syrone Wong --- shadowsocks-csharp/Controller/System/AutoStartup.cs | 4 ++-- shadowsocks-csharp/Util/Util.cs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/shadowsocks-csharp/Controller/System/AutoStartup.cs b/shadowsocks-csharp/Controller/System/AutoStartup.cs index 688ff362..c9f3d775 100644 --- a/shadowsocks-csharp/Controller/System/AutoStartup.cs +++ b/shadowsocks-csharp/Controller/System/AutoStartup.cs @@ -15,7 +15,7 @@ namespace Shadowsocks.Controller try { string path = Application.ExecutablePath; - runKey = Utils.OpenUserRegKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); + runKey = Utils.OpenRegKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); if ( runKey == null ) { Logging.Error( @"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Run" ); return false; @@ -54,7 +54,7 @@ namespace Shadowsocks.Controller try { string path = Application.ExecutablePath; - runKey = Utils.OpenUserRegKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); + runKey = Utils.OpenRegKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); if (runKey == null) { Logging.Error(@"Cannot find HKCU\Software\Microsoft\Windows\CurrentVersion\Run"); return false; diff --git a/shadowsocks-csharp/Util/Util.cs b/shadowsocks-csharp/Util/Util.cs index e6017432..60a748e2 100755 --- a/shadowsocks-csharp/Util/Util.cs +++ b/shadowsocks-csharp/Util/Util.cs @@ -211,7 +211,7 @@ namespace Shadowsocks.Util return new BandwidthScaleInfo(f, unit, scale); } - public static RegistryKey OpenUserRegKey( string name, bool writable ) + public static RegistryKey OpenRegKey( string name, bool writable, RegistryHive hive = RegistryHive.CurrentUser ) { // we are building x86 binary for both x86 and x64, which will // cause problem when opening registry key @@ -219,7 +219,7 @@ namespace Shadowsocks.Util if (name.IsNullOrEmpty()) throw new ArgumentException(nameof(name)); try { - RegistryKey userKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, + RegistryKey userKey = RegistryKey.OpenBaseKey(hive, Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32) .OpenSubKey(name, writable); return userKey; @@ -236,7 +236,7 @@ namespace Shadowsocks.Util } catch (ArgumentException ae) { - MessageBox.Show("OpenUserRegKey: " + ae.ToString()); + MessageBox.Show("OpenRegKey: " + ae.ToString()); return null; } }