A simple tiny class on C# learn your program to read/write ini files..
IniFile file = new IniFile("settings.ini");
file.Write("hello", "world", "main");
file.Write("2020", "Happy New Year!", "main");
This code creates file (settings.ini), and creates 2 lines under block [main]
[main]
hello=world
2020=Happy New Year!
In file settings.ini
:
[main]
hello=world
2020=Happy New Year!
Write this code:
IniFile file = new IniFile("settings.ini");
if(file.KeyExists("2020", "main"))
Console.WriteLine(file.Read("2020", "main"));
This code write in console Happy New Year!
Also, you can delete all line (key && value), for example:
IniFile file = new IniFile("settings.ini");
if(file.KeyExists("hello", "main"))
file.DeleteKey("hello", "main");
File settings.ini
before operation
[main]
hello=world
2020=Happy New Year!
File settings.ini
after operation
[main]
2020=Happy New Year!
Also, you can delete all values in section (block), for example:
IniFile file = new IniFile("settings.ini");
file.DeleteSection("main");
File settings.ini
before operation
; Comment
; Comment2
[main]
hello=world
2020=Happy New Year!
File settings.ini
after operation (all block main
has been removed)
; Comment
; Comment2
Default access for file - it's a Read And Write
, but you can set access manually in code to ReadOnly
or WriteOnly
. For example, if try write to file without rules, you can't do it and program was generated a new exceprion.
// read & write (default access)
IniFile fileFullAccess = new IniFile("settings.ini", FileAccess.ReadWrite);
// only read access
IniFile fileReadOnlyAccess = new IniFile("settings.ini", FileAccess.Read);
// only write access
IniFile fileWriteOnlyAccess = new IniFile("settings.ini", FileAccess.Write);
//exception on write with Readonly access
fileReadOnlyAccess.Write("hello", "world1", "main");