Get and set .ini file values c #
Suppose you have an .ini file with the following information:
[Data]
Count=5
The following functions will allow you to obtain and modify your configuration file in a simple way:
using System.Runtime.InteropServices;
using System.Text;
namespace qualityinfosolutions
{
public class Util
{
[DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section,
string key, string def, StringBuilder retVal,
int size, string filePath);
[DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section,
string key, string val, string filePath);
}
}
Mode of use
//Read Values
StringBuilder count = new StringBuilder();
string stringValue = "";
string file = "C:\\config.ini";
if (File.Exists(file))
{
Util.GetPrivateProfileString("Data",
"Count",
"",
count,
count.Capacity,
archivo);
stringValue = count.ToString();
}
else {
MsgError("Unable to find file " + file);
}
//Modify values
Util.WritePrivateProfileString("Data", "Count", 150, file);
I hope this is useful
regards