-
Notifications
You must be signed in to change notification settings - Fork 0
/
IniFile.cs
53 lines (44 loc) · 1.47 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
48
49
50
51
52
53
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
namespace KeyLoggerWithCSharp
{
public class IniFile
{
private string Section;
private string Path;
private string EXE = Assembly.GetExecutingAssembly().GetName().Name;
[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);
public IniFile(string IniPath = null, string IniSection = null)
{
Path = new FileInfo(IniPath ?? EXE + ".ini").FullName;
Section = IniSection ?? EXE;
}
public string Read(string Key)
{
var RetVal = new StringBuilder(255);
GetPrivateProfileString(Section ?? EXE, Key, "", RetVal, 255, Path);
return RetVal.ToString();
}
public void Write(string Key, string Value)
{
WritePrivateProfileString(Section ?? EXE, Key, Value, Path);
}
public void DeleteKey(string Key)
{
Write(Key, null);
}
public void DeleteSection(string Section = null)
{
Write(null, null);
}
public bool KeyExists(string Key)
{
return Read(Key).Length > 0;
}
}
}