134

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.

I'd like to use a custom ConfigurationSection, 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.

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

Any other approaches to this would be welcome!

newfurniturey
  • 37,556
  • 9
  • 94
  • 102
Mike Powell
  • 5,914
  • 4
  • 28
  • 28
  • 2
    See http://stackoverflow.com/questions/3912727/openmappedexeconfiguration-vs-openexeconfiguration/6599688#6599688 – Ohad Schneider Jul 06 '11 at 16:19
  • Does this answer your question? [Change default app.config at runtime](https://stackoverflow.com/questions/6150644/change-default-app-config-at-runtime) – Jim G. Jul 28 '21 at 14:08

9 Answers9

130

Try this:

System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap(strConfigPath); //Path to your config file
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
Ishmaeel
  • 14,138
  • 9
  • 71
  • 83
  • 1
    How can I get _programmatically_ **strConfigPath** value for my ASP.NET _WebForms_ application hosted in ***http://sub.domain.com/virtualDir2/*** and path ***C:\Portals\App1\v2*** and _config file_ in ***C:\Portals\App1\v2\web.config*** ? – Kiquenet Oct 07 '15 at 10:16
  • 2
    @Kiquenet: The point of the question is that strConfigPath is an _arbitrary_ location. In other words, _you_ decide what the path is, instead of relying on the framework to try to load a config file from its conventional location. I would assume [Server.MapPath](https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath(v=vs.110).aspx) would give you the absolute location for any files within your solution. – Ishmaeel Oct 07 '15 at 10:32
  • 2
    Maybe `var config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/web.config");` – Kiquenet Oct 07 '15 at 11:01
  • 1
    @Kiquenet Definitely. – Yarl Feb 19 '20 at 13:36
75

Another solution is to override the default environment configuration file path.

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.

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", <Full_Path_To_The_Configuration_File>);

Example:

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\Shared\app.config");

More details may be found at this blog.

Additionally, this other answer has an excellent solution, complete with code to refresh the app config and an IDisposable object to reset it back to it's original state. With this solution, you can keep the temporary app config scoped:

using(AppConfig.Change(tempFileName))
{
    // tempFileName is used for the app config during this context
}
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
Roi Shabtai
  • 2,981
  • 2
  • 31
  • 47
  • 1
    This also works to load web.config files. I use it to load a web.config instead of app.config for a task related console app. ;) – James Wilkins Mar 16 '15 at 17:48
  • 1
    This (and the other answers here) doesn't work for me. I had added the code in the program.cs - function: Main(). My config contains an assembly version redirection (see http://stackoverflow.com/questions/30165393/avoid-assembly-reference-problems-generally) but the redirection does not affect when the config is manually changed. – Vortex852456 May 11 '15 at 15:20
  • 1
    Did you use "APP_CONFIG_FILE"? – Roi Shabtai May 12 '15 at 07:14
  • This is a very clean and easy to implement solution that worked for me. – Caspian Canuck Apr 15 '21 at 19:42
42

Ishmaeel's answer generally does work, however I found one issue, which is that using OpenMappedMachineConfiguration 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:

ConfigurationFileMap fileMap = new ConfigurationFileMap(strConfigPath);
Configuration configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
MailSettingsSectionGroup thisMail = configuration.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;  // returns null

Basically, if you put a watch on the configuration.SectionGroups, you'll see that system.net is not registered as a SectionGroup, so it's pretty much inaccessible via the normal channels.

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.

<sectionGroup name="system.net" type="System.Net.Configuration.NetSectionGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
  <sectionGroup name="mailSettings" type="System.Net.Configuration.MailSettingsSectionGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <section name="smtp" type="System.Net.Configuration.SmtpSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </sectionGroup>
</sectionGroup>

I'm not sure the web application itself will run correctly after that, but you can access the sectionGroups correctly.

The second solution it is instead to open your web.config as an EXE configuration, which is probably closer to its intended function anyway:

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!

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.

Gavin
  • 9,855
  • 7
  • 49
  • 61
  • 1
    You can also use the ConfigurationManager.OpenExeConfiguration (String) overload for the same purpose. See: http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration3.aspx#t2_1 – Ohad Schneider Jul 06 '11 at 16:00
12

The accepted answer is wrong!!

It throws the following exception on accessing the AppSettings property:

Unable to cast object of type 'System.Configuration.DefaultSection' to type 'System.Configuration.AppSettingsSection'.

Here is the correct solution:

System.Configuration.ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "YourFilePath";
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
Jacob
  • 3,598
  • 4
  • 35
  • 56
  • 1
    yes, this is definitely the right answer! Thanks for posting your answer. – Fabio Milheiro Jun 17 '19 at 15:40
  • I think System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration is even more correct but it's not available for .NET Core so in that case this answer seems to get work done. – Risord Oct 08 '19 at 12:03
10

In addition to Ishmaeel's answer, the method OpenMappedMachineConfiguration() will always return a Configuration object. So to check to see if it loaded you should check the HasFile property where true means it came from a file.

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
Joseph Daigle
  • 47,650
  • 10
  • 49
  • 73
4

I provided the configuration values to word hosted .nET Compoent as follows.

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.

string sMsg = System.Configuration.ConfigurationManager.AppSettings["WSURL"];
abatishchev
  • 98,240
  • 88
  • 296
  • 433
1

For ASP.NET use WebConfigurationManager:

var config = WebConfigurationManager.OpenWebConfiguration("~/Sites/" + requestDomain + "/");
(..)
config.AppSettings.Settings["xxxx"].Value;
dwitvliet
  • 7,242
  • 7
  • 36
  • 62
  • the first parameter to OpenWebConfiguration is just the path (folder). To specify a filename you need a second parameter – Garr Godfrey Nov 16 '20 at 23:06
1

This should do the trick :

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "newAppConfig.config);

Source : https://www.codeproject.com/Articles/616065/Why-Where-and-How-of-NET-Configuration-Files

gatsby
  • 1,148
  • 11
  • 12
0

Use XML processing:

var appPath = AppDomain.CurrentDomain.BaseDirectory;
var configPath = Path.Combine(appPath, baseFileName);;
var root = XElement.Load(configPath);

// can call root.Elements(...)
JoelFan
  • 37,465
  • 35
  • 132
  • 205