forked from sbozzie/test-intel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.cs
More file actions
81 lines (76 loc) · 2.83 KB
/
Copy pathSettings.cs
File metadata and controls
81 lines (76 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using Microsoft.Win32;
using System;
using System.IO;
using System.Security;
namespace TestIntelReporter {
/// <summary>
/// Manages the storage of program settings in the registry.
/// </summary>
internal class Settings {
/// <summary>
/// There is very little specific to us, so keep the services
/// information on a shared TEST registry key.
/// </summary>
private const string KeyName = "SOFTWARE\\Test Alliance Please Ignore";
/// <summary>
/// The user's TEST authentication name.
/// </summary>
private const string UsernameKey = "Username";
/// <summary>
/// The user's TEST services password pre-hashed.
/// </summary>
private const string PasswordKey = "ServicesPasswordHash";
/// <summary>
/// Initializes a new instance of the <see cref="Settings"/>
/// class.
/// </summary>
/// <remarks>
/// Attempts to read out the values of keys already in the
/// registry.
/// </remarks>
public Settings() {
try {
using (var key = Registry.CurrentUser.OpenSubKey(KeyName)) {
if (key != null) {
this.Username = key.GetValue(UsernameKey) as string;
this.PasswordHash = key.GetValue(PasswordKey) as string;
}
}
} catch (SecurityException) {
// This really shouldn't happen...
} catch (IOException) {
// Again, really shouldn't happen...
} catch (UnauthorizedAccessException) {
// Nor this one...
}
}
/// <summary>
/// Gets or sets the user's AUTH username.
/// </summary>
public string Username { get; set; }
/// <summary>
/// Gets or sets the SHA1 hash of the user's services password.
/// </summary>
public string PasswordHash { get; set; }
/// <summary>
/// Saves the settings back into the registry.
/// </summary>
/// <returns>
/// <see langword="true"/> if the settings were successfully saved;
/// otherwise, <see langword="false"/>.
/// </returns>
public bool Save() {
try {
using (var key = Registry.CurrentUser.CreateSubKey(KeyName)) {
key.SetValue(UsernameKey, this.Username ?? String.Empty);
key.SetValue(PasswordKey, this.PasswordHash ?? String.Empty);
return true;
}
} catch (SecurityException) {
} catch (IOException) {
} catch (UnauthorizedAccessException) {
}
return false;
}
}
}