This repository has been archived by the owner on Dec 4, 2021. It is now read-only.
/
IniParser.cs
55 lines (46 loc) · 1.67 KB
/
IniParser.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace MusicBeePlugin
{
public class IniParser // revision 11
{
string _path;
string _exe = Assembly.GetExecutingAssembly().GetName().Name;
[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern long WritePrivateProfileString(string section, string key, string value, string filePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern int GetPrivateProfileString(string section, string key, string @default, StringBuilder retVal, int size, string filePath);
public IniParser(string iniPath = null)
{
_path = new FileInfo(iniPath ?? _exe + ".ini").FullName;
}
public string Read(string key, string section = null)
{
var retVal = new StringBuilder(255);
GetPrivateProfileString(section ?? _exe, key, "", retVal, 255, _path);
return retVal.ToString();
}
public void Write(string key, string value, string section = null)
{
WritePrivateProfileString(section ?? _exe, key, value, _path);
}
public void DeleteKey(string key, string section = null)
{
Write(key, null, section ?? _exe);
}
public void DeleteSection(string section = null)
{
Write(null, null, section ?? _exe);
}
public bool KeyExists(string key, string section = null)
{
return Read(key, section).Length > 0;
}
}
}