-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIniManager.cs
61 lines (48 loc) · 2.24 KB
/
IniManager.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
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace wp.dll.iniManager
{
public class IniManager
{
private string Path { get; } = AppDomain.CurrentDomain.BaseDirectory + @"config.ini";
public IniManager(string aPath) => Path = aPath;
public IniManager() { }
public string Get(string aSection, string aKey) => GetPrivateString(aSection, aKey);
public void Set(string aSection, string aKey, string aValue)
{
Create();
Set(aSection, aKey, aValue as object);
}
public void Set(string aSection, string aKey, object aValue)
{
Create();
WritePrivateString(aSection, aKey, aValue.ToString().Replace("False", "0").Replace("True", "1"));
}
public bool CheckExist() => System.IO.File.Exists(Path);
public void Create()
{
if (!CheckExist())
{
System.IO.File.Create(Path).Close();
}
}
#region private
private string GetPrivateString(string aSection, string aKey)
{
var buffer = new StringBuilder(Size);
GetPrivateString(aSection, aKey, null, buffer, Size, Path);
return buffer.ToString();
}
private void WritePrivateString(string aSection, string aKey, string aValue) =>
WritePrivateString(aSection, aKey, aValue, Path);
private const int Size = 1_024; //Максимальный размер (для чтения значения из файла)
//Импорт функции GetPrivateProfileString (для чтения значений) из библиотеки kernel32.dll
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString")]
private static extern int GetPrivateString(string section, string key, string def, StringBuilder buffer, int size, string path);
//Импорт функции WritePrivateProfileString (для записи значений) из библиотеки kernel32.dll
[DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileString")]
private static extern int WritePrivateString(string section, string key, string str, string path);
#endregion
}
}