PasswordStoreService.cs 827 B

123456789101112131415161718192021222324252627
  1. using Microsoft.Win32;
  2. namespace NetworkTool.Client.Services;
  3. public sealed class PasswordStoreService
  4. {
  5. private const string RegistryPath = @"Software\NetworkTool";
  6. private const string ValueName = "SavedPassword";
  7. public string LoadPassword()
  8. {
  9. using var key = Registry.CurrentUser.OpenSubKey(RegistryPath, writable: false);
  10. return key?.GetValue(ValueName) as string ?? string.Empty;
  11. }
  12. public void SavePassword(string password)
  13. {
  14. using var key = Registry.CurrentUser.CreateSubKey(RegistryPath);
  15. key.SetValue(ValueName, password, RegistryValueKind.String);
  16. }
  17. public void ClearPassword()
  18. {
  19. using var key = Registry.CurrentUser.OpenSubKey(RegistryPath, writable: true);
  20. key?.DeleteValue(ValueName, throwOnMissingValue: false);
  21. }
  22. }