-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniFile.cs
More file actions
64 lines (51 loc) · 1.8 KB
/
IniFile.cs
File metadata and controls
64 lines (51 loc) · 1.8 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace BugSplatCrashHandler
{
// See https://stackoverflow.com/questions/217902/reading-writing-an-ini-file
class IniFile
{
string Path;
[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 IniFile()
{
Path = "";
}
public IniFile(string IniPath)
{
Path = new FileInfo(IniPath ?? "BsSndRpt" + ".ini").FullName;
}
public string Read(string Key, bool required = false)
{
var RetVal = new StringBuilder(255);
GetPrivateProfileString("BugSplat", Key, "", RetVal, 255, Path);
if (RetVal.Length == 0 && required == true)
{
MessageBox.Show($"Missing required parameter {Key}");
Application.Exit();
}
return RetVal.ToString();
}
public void Write(string Key, string Value, string Section = null)
{
WritePrivateProfileString(Section ?? "BugSplat", Key, Value, Path);
}
public void DeleteKey(string Key, string Section = null)
{
Write(Key, "", Section ?? "BugSplat");
}
public void DeleteSection(string Section = null)
{
Write("", "", Section ?? "BugSplat");
}
public bool KeyExists(string Key)
{
return Read(Key).Length > 0;
}
}
}