class INIFile
{
private string filePath;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,
string key,
string val,
string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key,
string def,
StringBuilder retVal,
int size,
string filePath);
public INIFile(string filePath)
{
this.filePath = filePath;
}
public void Write(string section, string key, string value)
{
WritePrivateProfileString(section, key, value.ToLower(), this.filePath);
}
public string Read(string section, string key)
{
StringBuilder SB = new StringBuilder(255);
int i = GetPrivateProfileString(section, key, "", SB, 255, this.filePath);
return SB.ToString();
}
public string FilePath
{
get { return this.filePath; }
set { this.filePath = value; }
}
}
INIFile inif = new INIFile("D:\\config.ini");
inif.Write("Database", "Devs", "sa");
INIFile inif = new INIFile("D:\\config.ini");
Console.WriteLine("The Value is:" +inif.Read("Database", "Devs"));
//https://midnightprogrammer.net/post/readwrite-settings-to-ini-file-using-c/
Change INI File:
Easy way is to use WritePrivateProfileString and GetPrivateProfileString functions
of Kernel32.dll for reading and writing into INI file.
Example:
For Writing to INI:
[DllImport("kernel32.dll", EntryPoint = "WritePrivateProfileString")]
public static extern long WriteValueA(string strSection,
string strKeyName,
string strValue,
string strFilePath);
Usage:
WriteValueA("SectionToWrite", "KeyToWrite", "Value", @"D:\INIFile.ini");
For Reading from INI:
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString")]
public static extern int GetKeyValueA(string strSection,
string strKeyName,
string strEmpty,
StringBuilder RetVal,
int nSize,
string strFilePath);
Usage:
StringBuilder temp = new StringBuilder(255);
int i = GetKeyValueA("TargetSection", "KeyToRead", string.Empty, temp, 255, @"D:\INIFile.ini");
string sValue = temp.ToString(); //desired value of the key
CMD:
systeminfo
driverquery
tasklist
ipconfig /all
color a
color b
cls