-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniFile.cs
More file actions
45 lines (38 loc) · 1.37 KB
/
Copy pathIniFile.cs
File metadata and controls
45 lines (38 loc) · 1.37 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
using System.Runtime.InteropServices;
using System.Text;
namespace PresentationPrompter
{
public class IniFile
{
private readonly string _path;
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern int GetPrivateProfileString(
string section, string key, string defaultValue,
StringBuilder retVal, int size, string filePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern bool WritePrivateProfileString(
string section, string key, string value, string filePath);
public IniFile(string path)
{
_path = path;
}
public string Read(string section, string key, string defaultValue)
{
var sb = new StringBuilder(256);
GetPrivateProfileString(section, key, defaultValue, sb, 256, _path);
return sb.ToString();
}
public void Write(string section, string key, string value)
{
WritePrivateProfileString(section, key, value, _path);
}
public int ReadInt(string section, string key, int defaultValue)
{
string value = Read(section, key, defaultValue.ToString());
int result;
if (int.TryParse(value, out result))
return result;
return defaultValue;
}
}
}