How to test if a particular configuration section exists in an INI file using Nini library?

StackOverflow https://stackoverflow.com/questions/8002795

  •  21-02-2021
  •  | 
  •  

質問

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 !

正しい解決策はありません

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top