How to test if a particular configuration section exists in an INI file using Nini library?
-
21-02-2021 - |
Question
I have been looking for how to test if a particular INI file section exists but not so easy...
For example this INI file :
[MySQL]
server = "localhost"
user = "root"
In C# I would have to do the following to access the server and user keys and values :
IniConfigSource iniFile = new IniConfigSource("file.ini");
string serverHostName = iniFile.Configs["MySQL"].GetString("server");
but what if I want to test if [MySQL] section exists. I cannot use the following Contains
method which asks for a IConfig object not for a string :
if (iniFile.Configs.Contains(....))
Any idea on how to test this easily ?
Thanks !
No correct solution
OTHER TIPS
You could use the following code to test whether or not the ConfigCollection
of your IniConfigSource
instance contains a section with a given name:
IniConfigSource iniFile = new IniConfigSource("file.ini");
if(iniFile.Configs["MySQL"] != null)
{
// Ini-File contains section
...
}
else
{
// Ini-File does not contain section
...
}
Hope, this helps.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow