-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniFileHelper.cs
More file actions
74 lines (66 loc) · 2.51 KB
/
Copy pathIniFileHelper.cs
File metadata and controls
74 lines (66 loc) · 2.51 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
65
66
67
68
69
70
71
72
73
74
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace AtFocusVoice
{
public class IniFileHelper
{
private string _filePath;
public IniFileHelper(string filePath)
{
_filePath = filePath;
Debug.WriteLine($"INI File Path: {_filePath}");
}
public void WriteValue(string section, string key, string value)
{
bool result = WritePrivateProfileString(section, key, value, _filePath);
if (!result)
{
int error = Marshal.GetLastWin32Error();
Debug.WriteLine($"Failed to write INI file at path: {_filePath}. Error code: {error}. Message: {GetErrorMessage(error)}");
}
}
public string ReadValue(string section, string key, string defaultValue = "false")
{
StringBuilder result = new StringBuilder(255);
GetPrivateProfileString(section, key, defaultValue, result, 255, _filePath);
return result.ToString();
}
// 获取详细的错误信息
private string GetErrorMessage(int errorCode)
{
var buffer = new StringBuilder(256);
FormatMessage(
FormatMessageFlags.FORMAT_MESSAGE_FROM_SYSTEM | FormatMessageFlags.FORMAT_MESSAGE_IGNORE_INSERTS,
IntPtr.Zero,
errorCode,
0,
buffer,
buffer.Capacity,
IntPtr.Zero);
return buffer.ToString();
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int FormatMessage(
FormatMessageFlags dwFlags,
IntPtr lpSource,
int dwMessageId,
int dwLanguageId,
StringBuilder lpBuffer,
int nSize,
IntPtr Arguments
);
[Flags]
private enum FormatMessageFlags : int
{
FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000,
FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200
}
}
}