Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ishmaeel's answer generally does work, however I found one issue, which is that using <code>OpenMappedMachineConfiguration</code> seems to lose your inherited section groups from machine.config. This means that you can access your own custom sections (which is all the OP wanted), but not the normal system sections. For example, this code will not work:</p> <pre><code>ConfigurationFileMap fileMap = new ConfigurationFileMap(strConfigPath); Configuration configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap); MailSettingsSectionGroup thisMail = configuration.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup; // returns null </code></pre> <p>Basically, if you put a watch on the <code>configuration.SectionGroups</code>, you'll see that system.net is not registered as a SectionGroup, so it's pretty much inaccessible via the normal channels.</p> <p>There are two ways I found to work around this. The first, which I don't like, is to re-implement the system section groups by copying them from machine.config into your own web.config e.g.</p> <pre><code>&lt;sectionGroup name="system.net" type="System.Net.Configuration.NetSectionGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;sectionGroup name="mailSettings" type="System.Net.Configuration.MailSettingsSectionGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;section name="smtp" type="System.Net.Configuration.SmtpSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /&gt; &lt;/sectionGroup&gt; &lt;/sectionGroup&gt; </code></pre> <p>I'm not sure the web application itself will run correctly after that, but you can access the sectionGroups correctly.</p> <p>The second solution it is instead to open your web.config as an EXE configuration, which is probably closer to its intended function anyway:</p> <pre><code>ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = strConfigPath }; Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); MailSettingsSectionGroup thisMail = configuration.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup; // returns valid object! </code></pre> <p>I daresay none of the answers provided here, neither mine or Ishmaeel's, are quite using these functions how the .NET designers intended. But, this seems to work for me.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload