Override config file location for .NET class library?
I have a class library which I want to call from my .NET application as well as expose via COM and call from for example excel.
This class library needs to get some values from a config file. When referencing from my .NET application I can of course just put the stuff it needs in the app.config. But I can't do this when calling from excel.
Is there a way to override the loca开发者_Go百科tion for the config file when calling from excel?
Not exactly. What you can do is open a specific configuration file using ConfigurationManager.OpenExeConfiguration, and read configuration settings from there.
However, this is a good example of why it is not a good idea for class libraries to reference application configuration directly: libraries are designed to be reused across applications, and different applications may have different ways of specifying configuration options. This applies to more than just Excel.
Instead, you should design your library so that any options necessary to control it's behavior can be set as properties or passes in as method arguments, whichever is more appropriate to the option and the style of the API.
If you are using application settings and want to control where they are saved/loaded from, this answer has information on how to do this.
精彩评论