-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppSettings.cs
More file actions
82 lines (73 loc) · 2.57 KB
/
Copy pathAppSettings.cs
File metadata and controls
82 lines (73 loc) · 2.57 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
using System;
using System.IO;
using System.Web.Script.Serialization;
using Microsoft.Win32;
namespace KabuMSLauncher.Services
{
public class AppSettings
{
public string LoginId { get; set; } = string.Empty;
public string EncryptedPassword { get; set; } = string.Empty;
public string MarketSpeedExePath { get; set; } = string.Empty;
private static string SettingsDir =>
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "KabuMSLauncher");
private static string SettingsPath => Path.Combine(SettingsDir, "settings.json");
public static AppSettings Load()
{
try
{
if (File.Exists(SettingsPath))
{
var json = File.ReadAllText(SettingsPath);
var ser = new JavaScriptSerializer();
var s = ser.Deserialize<AppSettings>(json);
if (s != null) return s;
}
}
catch { /* fall through */ }
return new AppSettings();
}
public void Save()
{
try
{
if (!Directory.Exists(SettingsDir)) Directory.CreateDirectory(SettingsDir);
var ser = new JavaScriptSerializer();
File.WriteAllText(SettingsPath, ser.Serialize(this));
}
catch { /* non-critical */ }
}
}
public static class AutoStartRegistry
{
private const string RunKey = @"Software\Microsoft\Windows\CurrentVersion\Run";
private const string ValueName = "KabuMSLauncher";
public static bool IsEnabled()
{
using (var key = Registry.CurrentUser.OpenSubKey(RunKey, false))
{
return key != null && key.GetValue(ValueName) != null;
}
}
public static void Enable()
{
using (var key = Registry.CurrentUser.OpenSubKey(RunKey, true))
{
if (key == null) return;
var exe = System.Reflection.Assembly.GetEntryAssembly()?.Location;
if (!string.IsNullOrEmpty(exe))
{
key.SetValue(ValueName, "\"" + exe + "\"");
}
}
}
public static void Disable()
{
using (var key = Registry.CurrentUser.OpenSubKey(RunKey, true))
{
if (key == null) return;
if (key.GetValue(ValueName) != null) key.DeleteValue(ValueName);
}
}
}
}