0

I have a class library which needs some configuration to function. This class library is referenced by multiple applications (Multiple ASP.Net websites, and Windows Forms applications)

It is my understanding that it is possible to store the configuration in the library's app.config => myDll.dll.config file. See: Putting configuration information in a DLL, and C# Dll config file

My issue is that I don't want to manually handle copying the config file to the bin folder of every host assembly. Is there a mechanism in .Net to handle pairing of the dll to its config file so that the accompanying configuration is copiled along with the dll whereever it is distributed/referenced?

Community
  • 1
  • 1
Null
  • 93
  • 7

2 Answers2

1

If the config is the same for all instances of your dll, then I'd add it as an embedded resource, so it's part of your dll and not a separate file at all.

TO do this, either add it as a file resource to your Resources.resx file, or just add the file directly to your Project and then set its compile type (in the Properties window) to Embedded Resource.

You can then use Assembly.GetExecutingAssembly.GetManifestResourceNames() to list the names of the resources in your dll, and Assembly.GetExecutingAssembly.GetManifestResourceStream() to get a stream to read the file's data from. I'd probably use a simple homebrew XML format for my data and then an XmlTextReader/XmlDocument to read it very easily back in.

Jason Williams
  • 56,972
  • 11
  • 108
  • 137
  • Thanks, this would work; However should a change be required to the configuration a new dll would have to be compiled and distributed. – Null Nov 10 '10 at 19:31
  • @Null: An option: instead of just reading from the resource, check first if there is a config file in the Application.StartupPath - open that as your input stream in preference to the one embedded in your resources. If the file is missing, fallback to the resource. That way the defaults are cleanly embedded into the dll, but the user can add a file later to override settings if they need to. – Jason Williams Nov 12 '10 at 19:49
  • Have a look at this library which simplifies configuration madness https://github.com/aloneguid/config – Ivan G. Sep 29 '15 at 23:44
0

You'l have to deploy this .dll into GAC and put there the config file, all apps will first search the GAC when loanding a reference. Here is how you can deploy the dll + config.

Stefan P.
  • 9,489
  • 6
  • 29
  • 43