public class IniFile
{
private const uint StringBufferSize = 32768;
public IniFile(string filePath)
{
FilePath = filePath;
Enc = Encoding.Default;
}
public IniFile(string filePath, Encoding enc)
{
FilePath = filePath;
Enc = enc;
}
[DllImport("kernel32.dll",
EntryPoint = "GetPrivateProfileSectionNames")]
private static extern uint GetPrivateProfileSectionNames(
sbyte[] lpszReturnBuffer, uint nSize, string lpFileName);
public List<string> GetSectionNames()
{
sbyte[] lpszReturnBuffer = new sbyte[StringBufferSize];
uint len = GetPrivateProfileSectionNames(lpszReturnBuffer,
(uint)lpszReturnBuffer.Length, FilePath);
List<string> sections = new List<string>();
unsafe
{
fixed (sbyte* pBuf = lpszReturnBuffer)
{
uint i = 0;
while (i < len)
{
uint start = i;
uint length = 0;
while (i < len && lpszReturnBuffer[i] != 0)
{
++i;
}
length = i - start;
sections.Add(new string(pBuf,
(int)start, (int)length, Enc));
++i;
if (i < len && lpszReturnBuffer[i] == 0)
{
break;
}
}
}
}
return sections;
}
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSection")]
private static extern uint GetPrivateProfileSection(string lpAppName,
sbyte[] lpReturnedString, uint nSize, string lpFileName);
public List<string> GetSection(string section)
{
sbyte[] lpReturnedString = new sbyte[StringBufferSize];
uint len = GetPrivateProfileSection(section,
lpReturnedString, (uint)lpReturnedString.Length, FilePath);
List<string> items = new List<string>();
unsafe
{
fixed (sbyte* pBuf = lpReturnedString)
{
uint i = 0;
while (i < len)
{
uint start = i;
uint length = 0;
while (i < len && lpReturnedString[i] != 0)
{
++i;
}
length = i - start;
items.Add(new string(pBuf,
(int)start, (int)length, Enc));
++i;
if (i < len && lpReturnedString[i] == 0)
{
break;
}
}
}
}
return items;
}
public Dictionary<string, string> GetSetctionByPair(string section)
{
Dictionary<string, string> sectionDictionary = new Dictionary<string, string>();
List<string> sectionList = GetSection(section);
foreach (string value in sectionList)
{
string mark = "=";
char[] splitMark = mark.ToCharArray();
sectionDictionary.Add(value.Split(splitMark, 2)[0], value.Split(splitMark, 2)[1]);
}
return sectionDictionary;
}
[DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
private static extern uint GetPrivateProfileString(
string lpAppName, string lpKeyName, string lpDefault,
StringBuilder lpReturnedString, uint nSize, string lpFileName);
public string GetString(string section, string key, string def)
{
StringBuilder value = new StringBuilder((int)StringBufferSize);
GetPrivateProfileString(section, key,
def, value, StringBufferSize, FilePath);
return value.ToString();
}
[DllImport("kernel32")]
private static extern bool WritePrivateProfileSection(
string lpAppName,
string lpString,
string lpFileName
);
public bool WriteSection(string section, List<string> lstKeyValue)
{
string lpString = " ";
for (int i = 0; i < lstKeyValue.Count; ++i)
{
lpString += lstKeyValue[i];
lpString += "\0 ";
}
lpString += "\0 ";
return WritePrivateProfileSection(section, lpString, FilePath);
}
[DllImport("kernel32", EntryPoint = "WritePrivateProfileString")]
private static extern bool WritePrivateProfileString(string lpAppName,
string lpKeyName, string lpString, string lpFileName);
public bool WriteString(string section, string key, string value)
{
return WritePrivateProfileString(section, key, value, FilePath);
}
private string FilePath;
private Encoding Enc;
}
|