Reputation: 465
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 !
Upvotes: 1
Views: 3606
Reputation: 13070
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.
Upvotes: 2