-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppSettings.cs
67 lines (60 loc) · 2.42 KB
/
AppSettings.cs
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
using System;
using System.Configuration;
using System.Runtime.CompilerServices;
namespace Flithor_ReusableCodes
{
/// <summary>
/// Saved instantly configruation class
/// </summary>
public class AppSettings
{
public static string MyConfig
{
get => GetSetting() ?? "";
set => SetSetting(value);
}
public static string MySqlConnStr
{
get => GetConnStr() ?? "";
set => SetConnStr(value);
}
private static Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
private static string GetConnStr([CallerMemberName] string propName = "")
=> configuration.ConnectionStrings.ConnectionStrings[propName]?.ConnectionString;
private static void SetConnStr(string connStr, [CallerMemberName] string propName = "")
{
try
{
var connStrItem = configuration.ConnectionStrings.ConnectionStrings[propName];
if (connStrItem == null)
configuration.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(propName, connStr));
else
connStrItem.ConnectionString = connStr;
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
catch (Exception err)
{
//WriteDebugLog(err.ToString());
}
}
private static string GetSetting([CallerMemberName] string propName = "") { return configuration.AppSettings.Settings[propName]?.Value; }
private static void SetSetting(object value, [CallerMemberName] string propName = "") => SetSetting(value.ToString(), propName);
private static void SetSetting(string value, [CallerMemberName] string propName = "")
{
try
{
var confItem = configuration.AppSettings.Settings[propName];
if (confItem == null)
configuration.AppSettings.Settings.Add(propName, value);
else
confItem.Value = value;
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
catch (Exception err)
{
}
}
}
}