/// <summary>
/// Class that reads and writes INI files.
/// </summary>
public class INIHelper
{
//Related to read and write INI files.
[DllImport("", EntryPoint = "WritePrivateProfileString", CharSet = )]
public static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("", EntryPoint = "GetPrivateProfileString", CharSet = )]
public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
[DllImport("", EntryPoint = "GetPrivateProfileSectionNames", CharSet = )]
public static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, int nSize, string filePath);
[DllImport(" ", EntryPoint = "GetPrivateProfileSection", CharSet = )]
public static extern int GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string filePath);
/// <summary>
/// Write data to INI.
/// </summary>
/// <PARAM name="Section">Node name. </PARAM>
/// <PARAM name="Key">key name. </PARAM>
/// <PARAM name="Value">value name. </PARAM>
public static void Write(string Section, string Key, string Value, string path)
{
WritePrivateProfileString(Section, Key, Value, path);
}
/// <summary>
/// Read INI data.
/// </summary>
/// <PARAM name="Section">Node name. </PARAM>
/// <PARAM name="Key">key name. </PARAM>
/// <PARAM name="Path">value name. </PARAM>
/// <returns>corresponding value. </returns>
public static string Read(string Section, string Key, string path)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, path);
return ();
}
/// <summary>
/// Read all sections in an ini
/// </summary>
/// <param name="sections"></param>
/// <param name="path"></param>
/// <returns></returns>
public static int GetAllSectionNames(out string[] sections, string path)
{
int MAX_BUFFER = 32767;
IntPtr pReturnedString = (MAX_BUFFER);
int bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, path);
if (bytesReturned == 0)
{
sections = null;
return -1;
}
string local = (pReturnedString, (int)bytesReturned).ToString();
(pReturnedString);
//use of Substring below removes terminating null for split
sections = (0, - 1).Split('\0');
return 0;
}
/// <summary>
/// Get all the key and value combinations under a certain node
/// </summary>
/// <param name="section"></param>
/// <param name="keys"></param>
/// <param name="values"></param>
/// <param name="path"></param>
/// <returns></returns>
public static int GetAllKeyValues(string section, out string[] keys, out string[] values, string path)
{
byte[] b = new byte[65535];
GetPrivateProfileSection(section, b, , path);
string s = (b);
string[] tmp = ((char)0);
ArrayList result = new ArrayList();
foreach (string r in tmp)
{
if (r != )
(r);
}
keys = new string[];
values = new string[];
for (int i = 0; i < ; i++)
{
string[] item = result[i].ToString().Split(new char[] { '=' });
if ( == 2)
{
keys[i] = item[0].Trim();
values[i] = item[1].Trim();
}
else if ( == 1)
{
keys[i] = item[0].Trim();
values[i] = "";
}
else if ( == 0)
{
keys[i] = "";
values[i] = "";
}
}
return 0;
}
}