none
Can I have mutiple app.config files? RRS feed

  • Question

  • Hi,

    I have a WPF application developed in .net csharp 3.5 SP1 framework that will run on Windows XP.  We will have a single executable that will be used by different customers.  There will be several configuration settings that will reside in app.config that will be the same regardless of the customer.

    However there will be another group of settings that will be customer specific.  Because we have a single version of the executable rather than a customer specific version of the executable, these configuration settings will live and be managed outside of the application source code tree.  Thus when installing/deploying the application, the application executable will get installed, and then the specific customer configuration must also get installed so that the application can read those settings.

    What I am looking for then is to have a MyApp.exe.config file that has the generic settings, and something like a Customer.exe.config file that has the specific customer settings.  It would be desirable if I could setup my app.config file to load the Customer.exe.config file and access it via the Settings.Default properties in code just like the app.config settings.

    For example, in the application, I created a new Settings file that I called Customer.settings and I added some data.  So this object isolates my customer specific settings.  However now when I build my project, my app.config / MyApp.exe.config file contains a new section group named "Customer" and the specific settings I created are located in the userSettings section along with other data from app.config.  What would be nice is if the app.config group for Customer could refer to a file on disk and I could pull out the specific settings for Customer into that file.

    Is there a way to set this up with the config files?  Or do I have to write custom code to know how to load this data from a second config file?  Certainly I can just create an xml file and load it, but it would be nice if I could just use the System.Configuration classes that are already available.  Or is there a better way to accomplish the same thing?

    Thanks,

    Beth

     

     

     

    Thursday, April 1, 2010 6:34 PM

Answers

All replies

  • Yup.

    Right-click on the project in the Solution Explorer of Visual Studio. Select Add -> New Item... Select Settings File. Voila!

    Thursday, April 1, 2010 6:45 PM
  • You can have your app.config's appSettings section use the file  option to reference a second config file. 

     

    For details, and other alternatives, see this CodeProject article.


    Reed Copsey, Jr. - http://reedcopsey.com
    Thursday, April 1, 2010 6:51 PM
    Moderator
  • What I said is completely wrong. Creating a new settings file only creates a new section in the same old app.config file. That article is awesome!
    Thursday, April 1, 2010 7:23 PM
  • Yea, what Reed said. Also, suppose I had this in my config file:


    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <connectionStrings>
        <add name="ConnString" providerName="System.Data.SqlClient"
              connectionString="Data Source=MyServer;Initial Catalog=Northwind;Integrated Security=SSPI"/>
      </connectionStrings>
      <appSettings>
        <add key="SupportPath" value="C:\Documents and Settings\john.grove" />
        <add key="SupportEmail" value=someEmail@yahoo.com />   
      </appSettings>
    </configuration>

    Here is the difference on accessing them:
    String supportPath = ConfigurationManager.AppSettings["SupportPath"];
    String supportEmail = ConfigurationManager.AppSettings["SupportEmail"];
    String connString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Thursday, April 1, 2010 8:39 PM
  • Yes, I can use the file option in app.config under appSettings to referencee another file:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings file="customer.config" >
         <add key="customer" value="ME"/>
      </appSettings>
    </configuration>

    customer.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <appSettings>
       <add key="customer" value="MyCustomer" />
    </appSettings>

    Then in my application I can reference the 'customer' setting: string customer = ConfigurationManager.AppSettings["customer"];

    As the configuration is with the customer.config file, this value becomes 'MyCustomer' and if I remove the customer.config file then this value is the default "ME".

     

    However, prior to trying to use this alternate file, my app.config had been set up using the Settings.settings file.  It had a <configSections> group with sectionGroups for "applicationSettings" and "userSettings".  Then I had sections for <applicationSettings> and <userSettings>.   An example of the syntax of the applicationSetting looks like:

                <setting name="AutoLogin" serializeAs="String">
                    <value>False</value>
                </setting>

    Then in my code, I can access this setting:

    bool autoLogin = Settings.Default.AutoLogin

     

    However, it seems I cannot use the two schemes together.  If I comment out everything that is in my original app.config (configSections, etc) and add it the appSettings to reference the file, then the application starts.  However if I don't comment the original settings out, then I get a System.TypeInitializationException when the application starts.

    Is it possible to keep some settings in using the Settings.settings file, and also reference another file with additional settings?

    Thanks,

    Beth

    Friday, April 2, 2010 2:31 PM
  • Beth,

    Unfortunately, I believe you need to switch to using ConfigurationManager.AppSettings to access this, if you want to use external files.

     

    Reed


    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, April 2, 2010 3:33 PM
    Moderator
  • Hi Beth,

     

    I am writing to check the status of the issue on your side.  Would you mind letting us know the result of the suggestions? 

      

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, April 12, 2010 6:18 AM
    Moderator
  • Sorry for the late reply...  was out of town a few days, then playing catch up at work!

    Basically, as stated in my post on Friday, April 02, 2010 2:31 PM, I was able to use an external file using the suggestions as stated in the replies, but I could not combine those configuration settings with those defined in the Settings.settings file.

    My goal with all of this was to have common configuration settings, for use with all customers, defined in something like the Settings.settings file.  Then have a separate customer configuration file accessed by the application for those settings that are truly customer dependent.  The install of a particular customer would install the application (which would contain the common configuration settings) and also be responsible for deploying the customer specific configuration file.

    But I've abandoned that idea (and yes, I know I could easily write an xml parser to read in just a customer configuration file). 

    My approach is to now have everything in the app.config (common and customer specific) and we will have different versions per customer that our install will take care of.  I haven't gone down this path yet, so hopefully it will work ok.  Our customer install base is expected to be very small - if this were not so, it probably would not make any sense to manage the data in this way.

    Beth


    Friday, April 16, 2010 11:56 PM