-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIniFile.cs
40 lines (32 loc) · 1.27 KB
/
IniFile.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
using System.Runtime.InteropServices;
using System.Text;
namespace RHServerManager
{
public class IniFile
{
private readonly string _iniFilePath;
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
public IniFile(string iniFileName)
{
string appDirectory = AppDomain.CurrentDomain.BaseDirectory;
_iniFilePath = Path.Combine(appDirectory, iniFileName);
if (!File.Exists(_iniFilePath))
{
File.Create(_iniFilePath).Close();
}
}
public string ReadValue(string key, string defaultValue = "")
{
StringBuilder sb = new(255);
_ = GetPrivateProfileString("Option", key, defaultValue, sb, 255, _iniFilePath);
return sb.ToString();
}
public void WriteValue(string key, string value)
{
WritePrivateProfileString("Option", key, value, _iniFilePath);
}
}
}