This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIniFile.cs
47 lines (37 loc) · 1.8 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
41
42
43
44
45
46
47
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System;
// https://stackoverflow.com/questions/217902/reading-writing-an-ini-file
namespace ParallelPixivUtil2
{
// revision 11 + 1
public class IniFile
{
private readonly string Path;
private readonly string EXE = Assembly.GetExecutingAssembly().GetName().Name ?? "Program";
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern long WritePrivateProfileString(string Section, string? Key, string? Value, string FilePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);
[DllImport("kernel32")]
private static extern int GetLastError();
public IniFile(string? IniPath = null) => Path = new FileInfo(IniPath ?? EXE + ".ini").FullName;
public string Read(string Key, string? Section = null, bool silent = false)
{
var RetVal = new StringBuilder(1024);
_ = GetPrivateProfileString(Section ?? EXE, Key, "", RetVal, 1024, Path);
if (!silent)
{
int lastError = GetLastError();
if (lastError != 0)
throw new AggregateException($"Failed to get value from configuration: Key={Key}, Section={Section ?? "null"}, Error=0x{lastError:X8}");
}
return RetVal.ToString().Trim();
}
public void Write(string? Key, object? Value, string? Section = null) => WritePrivateProfileString(Section ?? EXE, Key, Value?.ToString()?.Trim(), 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) => Read(Key, Section, silent: true).Length > 0;
}
}