none
How to use .config file if DLL is in GAC

    Question

  • I like the enhancements in .Net 2.0 that allows config files to be used with DLL's. I have created a common library being used on many servers througout the company. To facilitate deployment and security, I placed the DLL in the GAC. However, my DLL has a configuration file. Can I reference the configuration if the DLL is in the GAC?
    Saturday, November 17, 2007 8:44 PM

Answers

  • It is no longer true that you cannot create config files for DLLs. As of .Net 2.0 you can do this. The config file has to be in the same location as the DLL with .config appended to the name (i.e. c:\mypath\mylibrary.dll and c:\mypath\mylibrary.dll.config). If you follow those rules then ConfigurationManager.OpenExeConfiguration(@"c:\mypath\mylibrary.dll") does work. I use that in my library.

    The limitation is that if you have other code that makes a reference copy of your DLL, the config file will not come with it. But if you turn off reference copying in dependent projects, then it will reference the DLL in its installed location where the config file will be found. Hopefully Microsoft addressed this in Visual Studio 2008 or a patch to 2005. Ideally if there is a config file present then it should be copied along with the DLL.

    You are right in that you cannot add text files to the GAC. However I did some more research and found that I can use the ConfigurationManager.OpenMappedExeConfiguration method to provide the full path name of the config file.
    Sunday, November 18, 2007 12:56 PM
  • You cannot create config files for dlls. If you use code in your dll to read data from config files, it will search for these data in the exe's config file or the web.config file for the application that loaded the dll.

    If you need to read data from config files for your dll, you may try to write code to manually open a text or xml file and read the configuration from it. I don't know if you can add text files to GAC.



    Sunday, November 18, 2007 7:38 AM

All replies

  • You cannot create config files for dlls. If you use code in your dll to read data from config files, it will search for these data in the exe's config file or the web.config file for the application that loaded the dll.

    If you need to read data from config files for your dll, you may try to write code to manually open a text or xml file and read the configuration from it. I don't know if you can add text files to GAC.



    Sunday, November 18, 2007 7:38 AM
  • It is no longer true that you cannot create config files for DLLs. As of .Net 2.0 you can do this. The config file has to be in the same location as the DLL with .config appended to the name (i.e. c:\mypath\mylibrary.dll and c:\mypath\mylibrary.dll.config). If you follow those rules then ConfigurationManager.OpenExeConfiguration(@"c:\mypath\mylibrary.dll") does work. I use that in my library.

    The limitation is that if you have other code that makes a reference copy of your DLL, the config file will not come with it. But if you turn off reference copying in dependent projects, then it will reference the DLL in its installed location where the config file will be found. Hopefully Microsoft addressed this in Visual Studio 2008 or a patch to 2005. Ideally if there is a config file present then it should be copied along with the DLL.

    You are right in that you cannot add text files to the GAC. However I did some more research and found that I can use the ConfigurationManager.OpenMappedExeConfiguration method to provide the full path name of the config file.
    Sunday, November 18, 2007 12:56 PM
  • Could you please elaborate with source code please. I'm stuck with the same problem. Appreciate any pointers. Thanks!

     

    Monday, November 19, 2007 6:25 PM
  • Just to elaborate, I have a DLL that needs to be placed in the GAC. I need the DLL to access the confiruation file that is built with the DLL. Any help greatly appreciated. I'm using .NET 2.0 framework.

     

     

     

    Monday, November 19, 2007 6:30 PM
  • I'm not sure if this will work in the GAC (sorry lack of time), but I know it works for a DLL assembly loaded under ASP.NET.

    Code Block

        internal static class Config
        {
            private static Configuration config;
            static Config()
            {
                Uri p = new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
                config = ConfigurationManager.OpenExeConfiguration(p.LocalPath);
            }

            internal static string GetString(string key)
            {
                KeyValueConfigurationElement element = config.AppSettings.Settings[key];
                if (element == null)
                    throw new SettingsPropertyNotFoundException(

    String.Format("Key '{0}' not found in AppSettings", key));

                return element.Value;
            }
        }


    Then you can access your config by doing:

    Code Block

    Config.GetString("test")


    As long as the file is named <YourAssembly>.dll.config, it's in the same folder with your dll, and it contains something like this:

    Code Block

    <?xml version="1.0"?>
    <configuration>
      <appSettings>
        <add key="test" value="Hello there!" />   
      </appSettings>
    </configuration>




    Wednesday, November 28, 2007 3:17 PM
  • How to add a dll and its config to the GAC

    What you do is add the .dll to c:\windows\assembly

    then goto a run prompt and head to c:\windows\assembly\gac_msil

    Browse for your directory (named the same as the dll). Open that directory, there will be a directory name looking very much a GUID inside that directory, open this. You should see your Dll. Drop your config file there with it... and presto.

    Makesure you are using ConfigurationManager.AppSettings[key] to retrieve the information from the dll (requires you actually add a reference to the .net System.Configuration dll).


    Thats it... easy as pi.
    • Proposed as answer by blakdayz Thursday, July 23, 2009 6:39 PM
    Thursday, July 23, 2009 6:39 PM
  • I wish to have application settings stored at User level.  

    My Application is an IE Toolbar, and is also within the GAC.

    From reading your posts my thoughts wander away from config files, and toward Registry settings instead, they seam to be easier to manage at user level for a GAC dll.

    Here the MS article I have just read ... I am going to give this a try ...

    Persisting Application Settings in the .NET Framework
    http://msdn.microsoft.com/en-us/library/ms973902.aspx

    I know my aplication will then have to provide the interface for modifying the values : so I will build a simple form .... 

    I also want to prompt for initial settings during MSI installation, and I have found an excellent article for doing this:
    Configure App.config Application Settings During MSI Install
    http://raquila.com/software/configure-app-config-application-settings-during-msi-install/
    sql dev
    • Edited by simon h jones Wednesday, September 02, 2009 1:10 PM Spelling
    Wednesday, September 02, 2009 1:09 PM