-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIniFiles.cs
More file actions
83 lines (68 loc) · 3.43 KB
/
IniFiles.cs
File metadata and controls
83 lines (68 loc) · 3.43 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
75
76
77
78
79
80
81
82
83
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace NBpostel
{
class IniFiles
{
string Path; //Имя файла.
[DllImport("kernel32", CharSet = CharSet.Auto)] // Подключаем kernel32.dll и описываем его функцию WritePrivateProfilesString
static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);
[DllImport("kernel32", CharSet = CharSet.Auto)] // Еще раз подключаем kernel32.dll, а теперь описываем функцию GetPrivateProfileString
static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern uint GetPrivateProfileSection(string lpAppName, IntPtr lpReturnedString, uint nSize, string lpFileName);
// С помощью конструктора записываем пусть до файла и его имя.
public IniFiles(string IniPath)
{
Path = new FileInfo(IniPath).FullName.ToString();
}
//Читаем ini-файл и возвращаем значение указного ключа из заданной секции.
public string ReadINI(string Section, string Key)
{
var RetVal = new StringBuilder(255);
GetPrivateProfileString(Section, Key, "", RetVal, 255, Path);
return RetVal.ToString();
}
//Записываем в ini-файл. Запись происходит в выбранную секцию в выбранный ключ.
public void Write(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, Path);
}
//Удаляем ключ из выбранной секции.
public void DeleteKey(string Key, string Section = null)
{
Write(Section, Key, null);
}
//Удаляем выбранную секцию
public void DeleteSection(string Section = null)
{
Write(Section, null, null);
}
//Проверяем, есть ли такой ключ, в этой секции
public bool KeyExists(string Section, string Key)
{
return ReadINI(Section, Key).Length > 0;
}
//читаем в массив все пары ключей-значнией в данной секции
public bool GetPrivateProfileSection(string appName, string fileName, out string[] section)
{
section = null;
if (!System.IO.File.Exists(fileName))
return false;
const uint MAX_BUFFER = 32767;
IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));
uint bytesReturned = GetPrivateProfileSection(appName, pReturnedString, MAX_BUFFER, fileName);
if ((bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0))
{
Marshal.FreeCoTaskMem(pReturnedString);
return false;
}
string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)(bytesReturned - 1));
section = returnedString.Split('\0');
Marshal.FreeCoTaskMem(pReturnedString);
return true;
}
}
}