C# DLL config file

后端 未结 17 2288
梦毁少年i
梦毁少年i 2020-11-22 05:05

Im trying to add an app.config file to my DLL, but all attempts have failed.

According to MusicGenesis in \'Putting configuration information in a DLL\' this should

相关标签:
17条回答
  • 2020-11-22 05:25

    Since the assembly resides in a temporary cache, you should combine the path to get the dll's config:

    var appConfig = ConfigurationManager.OpenExeConfiguration(
        Path.Combine(Environment.CurrentDirectory, Assembly.GetExecutingAssembly().ManifestModule.Name));
    
    0 讨论(0)
  • 2020-11-22 05:25

    As Marc says, this is not possible (although Visual Studio allows you to add an application configuration file in a class library project).

    You might want to check out the AssemblySettings class which seems to make assembly config files possible.

    0 讨论(0)
  • 2020-11-22 05:26

    The full solution is not often found in one place ...

    1) Create an app config file and name it "yourDllName.dll.config"
    2) Right click on the config file created above in VS Solution Explorer, click properties
    --- set "Build Action" = Content
    --- set "Copy To Output Directory" = Always
    3) Add an appSettings section to the configuration file (yourDllName.dll.config) with your yourKeyName and yourKeyValue

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="yourKeyName" value="yourKeyValue"/>
      </appSettings>
    </configuration>
    

    4) Add System.Configuration to your dll/class/project references
    5) Add the using statements to your code where you intend to access the config setting

    using System.Configuration;
    using System.Reflection;
    

    6) To access the value

    string keyValue = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location).AppSettings.Settings["yourKeyName"].Value;
    

    7) rejoice, it works

    IMHO, this should only be used when developing a new dll/library.

    #if (DEBUG && !FINALTESTING)
       string keyValue = ConfigurationManager.OpenExeConfiguration...(see 6 above)
    #else
       string keyValue = ConfigurationManager.AppSettings["yourKeyName"];
    #endif
    

    The config file ends up being a great reference, for when you add the dll's appSettings to your actual application.

    0 讨论(0)
  • 2020-11-22 05:29

    I've found what seems like a good solution to this issue. I am using VS 2008 C#. My solution involves the use of distinct namespaces between multiple configuration files. I've posted the solution on my blog: http://tommiecarter.blogspot.com/2011/02/how-to-access-multiple-config-files-in.html.

    For example:

    This namespace read/writes dll settings:

    var x = company.dlllibrary.Properties.Settings.Default.SettingName;
    company.dlllibrary.Properties.Settings.Default.SettingName = value;
    

    This namespace read/writes the exe settings:

    company.exeservice.Properties.Settings.Default.SettingName = value;
    var x = company.exeservice.Properties.Settings.Default.SettingName;
    

    There are some caveats mentioned in the article. HTH

    0 讨论(0)
  • 2020-11-22 05:34

    When using ConfigurationManager, I'm pretty sure it is loading the process/AppDomain configuration file (app.config / web.config). If you want to load a specific config file, you'll have to specifically ask for that file by name...

    You could try:

    var config = ConfigurationManager.OpenExeConfiguration("foo.dll");
    config.ConnectionStrings. [etc]
    
    0 讨论(0)
提交回复
热议问题