Have you ever worked with a .NET COM callable DLL and wanted to use a configuration file for your DLL instead of using a text file to store application settings, connection strings and such — or instead of hard-coding these?

The first time I tried using a configuration file for my COM library, let's call it myProj.dll, I was surprised to see that when I called my library from inside ASP the config file that my DLL was looking for when trying to get the settings with ConfigMananger was w3wp.exe.config, instead of my myProj.dll.config where I had stored all my settings.

Initially, I looked for solutions on the Internet and I found that people actually created a w3wp.exe.config and stored it in the same folder as w3wp.exe. Though, I don't think this is a feasible solution if you have more than one COM callable library.

I also tried to overwrite the path of the config file for the Application Domain with:

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE","C:\myProj.dll.config")

as in this post, but for some reason this wasn't always working properly.

The solution to my problem proved to be much more simple. First, I needed to determine the path for the running assembly as in this example:

Dim path As String path
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)

From this I could easily get the name and the path, which would yield something like:

Dim fullName As String
fullName = C:\myProj.dll

Then, no matter where I store and register my assembly using:

regasm /tlb /codebase C:\myProj.dll

I will be able to open the associated config file with:

Dim config As System.Configuration.Configuration
config = ConfigurationManager.OpenExeConfiguration(fullName)

From here on I can read my config file using ConfigManager without problems.

Share