DataDiagnosticsSettings.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using Microsoft.Win32; 7 8 namespace Microsoft.PowerToys.Telemetry 9 { 10 public static class DataDiagnosticsSettings 11 { 12 private static readonly string DataDiagnosticsRegistryKey = @"HKEY_CURRENT_USER\Software\Classes\PowerToys\"; 13 private static readonly string DataDiagnosticsRegistryValueName = @"AllowDataDiagnostics"; 14 private static readonly string DataDiagnosticsDataDiagnosticsUserActionRegistryValueName = @"DataDiagnosticsUserAction"; 15 private static readonly string DataDiagnosticsDataDiagnosticsViewDataRegistryValueName = @"DataDiagnosticsViewEnabled"; 16 17 public static bool GetEnabledValue() 18 { 19 object registryValue = null; 20 try 21 { 22 registryValue = Registry.GetValue(DataDiagnosticsRegistryKey, DataDiagnosticsRegistryValueName, 0); 23 24 if (registryValue is not null) 25 { 26 return (int)registryValue == 1 ? true : false; 27 } 28 } 29 catch 30 { 31 } 32 33 return false; 34 } 35 36 public static void SetEnabledValue(bool value) 37 { 38 try 39 { 40 Registry.SetValue(DataDiagnosticsRegistryKey, DataDiagnosticsRegistryValueName, value ? 1 : 0); 41 } 42 catch (Exception) 43 { 44 } 45 } 46 47 public static bool GetUserActionValue() 48 { 49 object registryValue = null; 50 try 51 { 52 registryValue = Registry.GetValue(DataDiagnosticsRegistryKey, DataDiagnosticsDataDiagnosticsUserActionRegistryValueName, 0); 53 } 54 catch 55 { 56 } 57 58 if (registryValue is not null) 59 { 60 return (int)registryValue == 1 ? true : false; 61 } 62 63 return false; 64 } 65 66 public static void SetUserActionValue(bool value) 67 { 68 try 69 { 70 Registry.SetValue(DataDiagnosticsRegistryKey, DataDiagnosticsDataDiagnosticsUserActionRegistryValueName, value ? 1 : 0); 71 } 72 catch (Exception) 73 { 74 } 75 } 76 77 public static bool GetViewEnabledValue() 78 { 79 object registryValue = null; 80 try 81 { 82 registryValue = Registry.GetValue(DataDiagnosticsRegistryKey, DataDiagnosticsDataDiagnosticsViewDataRegistryValueName, 0); 83 } 84 catch 85 { 86 } 87 88 if (registryValue is not null) 89 { 90 return (int)registryValue == 1 ? true : false; 91 } 92 93 return false; 94 } 95 96 public static void SetViewEnabledValue(bool value) 97 { 98 try 99 { 100 Registry.SetValue(DataDiagnosticsRegistryKey, DataDiagnosticsDataDiagnosticsViewDataRegistryValueName, value ? 1 : 0); 101 } 102 catch (Exception) 103 { 104 } 105 } 106 } 107 }