.NET configuration file: how to check if ConfigSection is present
Consider:
Line:
<section name="unity" />
Block:
<unity>
<typeAliases />
<containers />
</unity>
Say the line is available in the .config file while the block is missing.
How to programmatically check if a block exists or not?
[EDIT]
For those who are geniuses who quickly noted a negative question:
I have already tried ConfigurationManager.GetSection()
and
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var section = config.GetSection("unity");
var sInfo = section.SectionInformation;
var isDeclared = sInfo.IsDeclared;
Correct me if I am wrong, the above does not return zero if defined <configSections>(even if the actual unit block is missing).
I found this post looking for an answer to this post. I thought I would be back and send the answer now, when I resolved it.
ConfigurationSection ConfigurationElement, ElementInformation, , Deserialization.
ConfigurationSection . ConfigurationSection ConfigurationElement:
//After Deserialization
if(!customSection.ElementInformation.IsPresent)
Console.WriteLine("Section Missing");
, - , ( , "PropName" ), PropName ElementInformation IsPresent:
if(!customSection.propName.ElementInformation.IsPresent)
Console.WriteLine("Configuration Element was not found.");
, , <configSections> , :
CustomSection mySection =
config.GetSection("MySection") as CustomSection;
if(mySection == null)
Console.WriteLine("ConfigSection 'MySection' was not defined.");
-,
ConfigurationManager.GetSection. null, .
: System.Object
ConfigurationSection, null, .
if (ConfigurationManager.GetSection("unity") == null)
{
Console.WriteLine("Section does not exist");
}
, , ConfigurationManager.GetSection(). , , , name= ""? , - :
ConfigurationManager.GetSection("unity/unity");
cs, , - , RB.
*** EDIT ***
, , , :
ConfigurationSection section = ConfigurationManager.GetSection("unity");
System.Xml.XmlDocument xmlData = new System.Xml.XmlDocument();
xmlData.LoadXml(section);
XmlNode Found = null;
foreach (System.Xml.XmlNode node in xmlData.ChildNodes)
{
if (node.HasChildNodes)
{
Found = FindNode(node.ChildNodes, "unity");
}
}
if(Found == null) //do something
xml reader children "". IConfigurationSectionHandler, CreateMethod node ,
public class CustomConfigurationSectionHandler : IConfigurationSectionHandler {
public object Create(object parent,
object configContext, System.Xml.XmlNode section)
{
//check if section is a line , if yes return null
if( section ...)
{ return null; }
//otherwise return whatever result you want :)
else { }
}
}
, , ConfigurationManager.GetSection("unity") .
, ConfigurationManager.GetSection Method.
if(ConfigurationManager.GetSection(sectionName) == null)
{
// do something
}
You can open the configuration file and try to access the configuration by its name.
CustomSection customSection;
// Get the current configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
// Create the section entry
// in the <configSections> and the
// related target section in <configuration>.
if (config.Sections["CustomSection"] == null)
{