From 5341c7e1049f12e1a4e5af16e782026098746703 Mon Sep 17 00:00:00 2001 From: Juan Carlos Hernandez Ramos Date: Wed, 20 Dec 2017 18:01:06 -0500 Subject: [PATCH] CatchRegistryKey --- .../Program.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Tulpep.NetworkAutoSwitch.ProxyService/Program.cs b/Tulpep.NetworkAutoSwitch.ProxyService/Program.cs index d74b22f..58f7f32 100755 --- a/Tulpep.NetworkAutoSwitch.ProxyService/Program.cs +++ b/Tulpep.NetworkAutoSwitch.ProxyService/Program.cs @@ -110,15 +110,22 @@ private static void StartService(string serviceName, int timeoutMilliseconds, Pr service.Start(new string[] { "-p", priority.ToString() }); service.WaitForStatus(ServiceControllerStatus.Running, timeout); Logging.WriteConsoleMessage("Service running"); - - using (RegistryKey hkcu = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) + + try { - using (RegistryKey proxyAutoSwitch = hkcu.CreateSubKey(Constants.KEY_CONFIG_PATH, RegistryKeyPermissionCheck.Default)) + using (RegistryKey hkcu = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) { - string config = priority == Priority.Wired ? Decimal.One.ToString() : Decimal.Zero.ToString(); - proxyAutoSwitch.SetValue(Constants.KEY_CONFIG_NAME, config, RegistryValueKind.String); + using (RegistryKey proxyAutoSwitch = hkcu.CreateSubKey(Constants.KEY_CONFIG_PATH, RegistryKeyPermissionCheck.Default)) + { + string config = priority == Priority.Wired ? Decimal.One.ToString() : Decimal.Zero.ToString(); + proxyAutoSwitch.SetValue(Constants.KEY_CONFIG_NAME, config, RegistryValueKind.String); + } } } + catch (Exception e) + { + Logging.WriteMessageEventViewerError(Constants.SERVICE_NAME, e.Message); + } } private static void ExtractProxyEnabler()