-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.cs
More file actions
121 lines (112 loc) · 4.01 KB
/
Settings.cs
File metadata and controls
121 lines (112 loc) · 4.01 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using Newtonsoft.Json;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace SimpleBackup
{
/// <summary>
/// Used for saving and reading settings to/from an external settings file
/// </summary>
public static class Settings
{
public static SettingsData data = new SettingsData();
private static readonly string fileName = "settings.txt";
public static void Save()
{
try
{
string directory = GetDirectory();
if (directory != null)
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
string fileData = JsonConvert.SerializeObject(data);
File.WriteAllText(Path.Combine(directory, fileName), fileData);
}
}
catch (Exception e)
{
Log.WriteLine(e.Message);
}
}
public static void Load()
{
try
{
string directory = GetDirectory();
if (directory != null && File.Exists(Path.Combine(directory, fileName)))
{
string fileData = File.ReadAllText(Path.Combine(directory, fileName));
data = JsonConvert.DeserializeObject<SettingsData>(fileData);
}
}
catch (Exception e)
{
Log.WriteLine(e.Message);
}
}
/// <summary>
/// Encrypts any input value
/// </summary>
/// <typeparam name="T">The type of the value</typeparam>
/// <param name="value">The value to be encrypted</param>
/// <returns>The value encrypted to a string</returns>
public static string Encrypt<T>(T value)
{
string text = value.ToString();
if (!string.IsNullOrEmpty(text))
{
return Convert.ToBase64String(ProtectedData.Protect(Encoding.Unicode.GetBytes(text), null, DataProtectionScope.CurrentUser));
}
return null;
}
/// <summary>
/// Decrypts the encrypted input value as an integer
/// </summary>
/// <param name="encryptedText">The encrypted value</param>
/// <returns>The input value decrypted to an integer</returns>
public static int DecryptInt(string encryptedText)
{
string returnText = DecryptString(encryptedText);
if (!string.IsNullOrEmpty(returnText))
{
return int.Parse(returnText);
}
return -1;
}
/// <summary>
/// Decrypts the encrypted input value as a boolean
/// </summary>
/// <param name="encryptedText">The encrypted value</param>
/// <returns>The input value decrypted to a boolean</returns>
public static bool DecryptBool(string encryptedText)
{
string returnText = DecryptString(encryptedText);
if (!string.IsNullOrEmpty(returnText))
{
return bool.Parse(returnText);
}
return false;
}
/// <summary>
/// Decrypts the encrypted input value as a string
/// </summary>
/// <param name="encryptedText">The encrypted value</param>
/// <returns>The input value decrypted to a string</returns>
public static string DecryptString(string encryptedText)
{
if (!string.IsNullOrEmpty(encryptedText))
{
return Encoding.Unicode.GetString(ProtectedData.Unprotect(Convert.FromBase64String(encryptedText), null, DataProtectionScope.CurrentUser));
}
return "";
}
public static string GetDirectory()
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SimpleBackup");
}
}
}