C# COM Component Fails To Read Config When Loaded

2019-07-23 23:13发布

问题:

I have a COM component written in C# that makes calls to ConfigurationManager.AppSettings.Get(...). This component is instantiated inside an unmanaged C++ application.

Every time, ConfigurationManager.AppSettings is unable to read the configuration file.

There might be two problems.

1) What should the app.config be called when the app is running (i.e. the name of the C++ exe with .config on the end, or the name of my C# class library with .config on the end).

2) Where should the config file live (i.e. in the same folder as the C# COM Component, or in the same folder as the C++ exe

I've tried various calls to ConfigurationManager.OpenExecConfiguration; all failed.

What I want ideally, is for it to just work with no code changes. Failing that, a judicious call of OpenExeConfiguration so that I can still use ConfigurationManager.AppSettings.Get(...)

This might be the tip of the iceberg because I'd like to use log4net in the C# COM Component which uses a custom config section.

Whilst I dont mind changing my own code to use access settings via a config object, I can't change the log4net code.

Anyone else hit this before? Any ideas?

回答1:

If you run procmon ( http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx ) while your app is running can you see the *.config file that it is looking for?



回答2:

Usually, the config file is in the same place as where the C# binary is located....when you run a build on the project that contains the app.config, that gets converted to app.exe.config where app is the name of the project solution, and hence the .EXE file when loaded and executed by the .NET framework, reads the config file from the same directory where the .EXE reside in.