<p>I'm developing a data access component that will be used in a website that contains a mix of classic ASP and ASP.NET pages, and need a good way to manage its configuration settings.</p>

<p>I'd like to use a custom <code>ConfigurationSection</code>, and for the ASP.NET pages this works great.  But when the component is called via COM interop from a classic ASP page, the component isn't running in the context of an ASP.NET request and therefore has no knowledge of web.config.</p>

<p>Is there a way to tell the <code>ConfigurationManager</code> to just load the configuration from an arbitrary path (e.g. <code>..\web.config</code> if my assembly is in the <code>/bin</code> folder)?  If there is then I'm thinking my component can fall back to that if the default <code>ConfigurationManager.GetSection</code> returns <code>null</code> for my custom section.</p>

<p>Any other approaches to this would be welcome!</p>

[{'answer_id': 4746, 'author': 'Ishmaeel', 'author_id': 227, 'author_profile': 'https://Stackoverflow.com/users/227', 'pm_score': 8, 'selected': True, 'text': '<p>Try this:</p>\n\n<pre><code>System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap(strConfigPath); //Path to your config file\nSystem.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);\n</code></pre>\n'}, {'answer_id': 5965, 'author': 'Joseph Daigle', 'author_id': 507, 'author_profile': 'https://Stackoverflow.com/users/507', 'pm_score': 3, 'selected': False, 'text': "<p>In addition to Ishmaeel's answer, the method <code>OpenMappedMachineConfiguration()</code> will always return a <code>Configuration</code> object. So to check to see if it loaded you should check the <code>HasFile</code> property where true means it came from a file.</p>\n"}, {'answer_id': 3818417, 'author': 'Gavin', 'author_id': 78216, 'author_profile': 'https://Stackoverflow.com/users/78216', 'pm_score': 5, 'selected': False, 'text': '<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>\n\n<pre><code>ConfigurationFileMap fileMap = new ConfigurationFileMap(strConfigPath);\nConfiguration configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);\nMailSettingsSectionGroup thisMail = configuration.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;  // returns null\n</code></pre>\n\n<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>\n\n<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>\n\n<pre><code>&lt;sectionGroup name="system.net" type="System.Net.Configuration.NetSectionGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt;\n  &lt;sectionGroup name="mailSettings" type="System.Net.Configuration.MailSettingsSectionGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt;\n    &lt;section name="smtp" type="System.Net.Configuration.SmtpSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /&gt;\n  &lt;/sectionGroup&gt;\n&lt;/sectionGroup&gt;\n</code></pre>\n\n<p>I\'m not sure the web application itself will run correctly after that, but you can access the sectionGroups correctly.</p>\n\n<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>\n\n<pre><code>ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = strConfigPath };\nConfiguration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);\nMailSettingsSectionGroup thisMail = configuration.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;  // returns valid object!\n</code></pre>\n\n<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>\n'}, {'answer_id': 4157995, 'author': 'Iftikhar Ali', 'author_id': 504911, 'author_profile': 'https://Stackoverflow.com/users/504911', 'pm_score': 2, 'selected': False, 'text': '<p>I provided the configuration values to word hosted .nET Compoent as follows.</p>\n\n<p>A .NET Class Library component being called/hosted in MS Word.  To provide configuration values to my component, I created winword.exe.config in C:\\Program Files\\Microsoft Office\\OFFICE11 folder.  You should be able to read configurations values like You do in Traditional .NET.</p>\n\n<pre><code>string sMsg = System.Configuration.ConfigurationManager.AppSettings["WSURL"];\n</code></pre>\n'}, {'answer_id': 14246260, 'author': 'Roi Shabtai', 'author_id': 739558, 'author_profile': 'https://Stackoverflow.com/users/739558', 'pm_score': 6, 'selected': False, 'text': '<p>Another solution is to override the default environment configuration file path.</p>\n<p>I find it the best solution for the of non-trivial-path configuration file load, specifically the best way to attach configuration file to dll.</p>\n<pre><code>AppDomain.CurrentDomain.SetData(&quot;APP_CONFIG_FILE&quot;, &lt;Full_Path_To_The_Configuration_File&gt;);\n</code></pre>\n<p>Example:</p>\n<pre><code>AppDomain.CurrentDomain.SetData(&quot;APP_CONFIG_FILE&quot;, @&quot;C:\\Shared\\app.config&quot;);\n</code></pre>\n<p>More details may be found at <a href="http://web.archive.org/web/20161222190702/http://www.testautomationguy.com/2013/01/override-default-application-config.html" rel="nofollow noreferrer">this blog</a>.</p>\n<p>Additionally, <a href="https://stackoverflow.com/a/6151688/503826">this other answer</a> has an excellent solution, complete with code to refresh\nthe app config and an <code>IDisposable</code> object to reset it back to it\'s original state. With this\nsolution, you can keep the temporary app config scoped:</p>\n<pre><code>using(AppConfig.Change(tempFileName))\n{\n    // tempFileName is used for the app config during this context\n}\n</code></pre>\n'}, {'answer_id': 24745637, 'author': 'Javier Cañon', 'author_id': 3838468, 'author_profile': 'https://Stackoverflow.com/users/3838468', 'pm_score': 1, 'selected': False, 'text': '<p>For ASP.NET use WebConfigurationManager:</p>\n\n<pre><code>var config = WebConfigurationManager.OpenWebConfiguration("~/Sites/" + requestDomain + "/");\n(..)\nconfig.AppSettings.Settings["xxxx"].Value;\n</code></pre>\n'}, {'answer_id': 35280447, 'author': 'JoelFan', 'author_id': 16012, 'author_profile': 'https://Stackoverflow.com/users/16012', 'pm_score': 0, 'selected': False, 'text': '<p>Use XML processing:</p>\n\n<pre><code>var appPath = AppDomain.CurrentDomain.BaseDirectory;\nvar configPath = Path.Combine(appPath, baseFileName);;\nvar root = XElement.Load(configPath);\n\n// can call root.Elements(...)\n</code></pre>\n'}, {'answer_id': 51765082, 'author': 'gatsby', 'author_id': 5775048, 'author_profile': 'https://Stackoverflow.com/users/5775048', 'pm_score': 1, 'selected': False, 'text': '<p>This should do the trick :</p>\n\n<pre><code>AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "newAppConfig.config);\n</code></pre>\n\n<p>Source : <a href="https://www.codeproject.com/Articles/616065/Why-Where-and-How-of-NET-Configuration-Files" rel="nofollow noreferrer">https://www.codeproject.com/Articles/616065/Why-Where-and-How-of-NET-Configuration-Files</a></p>\n'}, {'answer_id': 55241920, 'author': 'Jacob', 'author_id': 665783, 'author_profile': 'https://Stackoverflow.com/users/665783', 'pm_score': 4, 'selected': False, 'text': '<p><strong>The accepted answer is wrong!!</strong></p>\n\n<p>It throws the following exception on accessing the AppSettings property:</p>\n\n<blockquote>\n  <p>Unable to cast object of type \'System.Configuration.DefaultSection\' to type \'System.Configuration.AppSettingsSection\'.</p>\n</blockquote>\n\n<p><strong>Here is the correct solution:</strong></p>\n\n<pre><code>System.Configuration.ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();\nfileMap.ExeConfigFilename = "YourFilePath";\nSystem.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/4738', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/205/']