I have a .NET dll which needs to read it's config settings from it's config file. Usually, the config file is placed in the same directory as the DLL. But how do i read the config file if the DLL is GAC'ed, because I can put only the DLLs in the GAC, and not it's config files.
相关问题
- slurm: use a control node also for computing
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
You can make use of
AppDomain.CurrentDomain.BaseDirectory
since the DLL library will not be executed by itself you just need to get Executable file directory who is calling himSomething like:
Here my executable is in folder
bin\Debug
and inside the folder i have a folder calledConfig
where i have the xml config fileconfig.xml
. So thesFileName
will provide you the relative path to the config file as\bin\Debug\Config\config.xml
Does the user need to configure the Dll? If so, then the DLL should be using configuration settings from the app.config file, not it's own config. The app.config file should be stored in the same directory as the application. If not, then you could go a couple of different ways. You could make changes to the machine.config file so that your Dll can find them there. I would not do this. Alternatively, you can store the configuration in a settings class. These can be overridden via configuration, but your defaults will be set in the generated code for the settings class via attributes and so the absence of a configuration file will not affect your Dll when the defaults are all that are required.
I agree with tvanfosson the Gac'ed dll will read from the application's path. But you could also inform the dll which is the path in this way: