none
Trouble accessing conifguration file settings RRS feed

  • Question

  • I am experimenting with using app.config files. Ultimately I would like to have multiple applications (potentially installed to different locations) to:

    • Have their own app.config files for configuration items that are specific to the individual application.
    • Be able to share a common config file for settings that should be common across the applications.

    (At the moment I am concerned about application settings rather than per user configurable settings.)

    I have tried a very simple application that has its own configuration file and also loads another file (which represents the common settings.) Here is the complete code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Configuration;
    using System.Collections.Specialized ;
    
    namespace ConsoleAppWithSettings
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			String MyAppSetting = Properties.Settings.Default.AppSpecificSetting ;
    			Console.WriteLine(MyAppSetting);
    			System.Configuration.Configuration Config = ConfigurationManager.OpenExeConfiguration("App1.config") ;
    			var AS = Config.AppSettings ;
    			var S1 = AS.Settings["S1"].Value ;
    			
    
    			Console.WriteLine(S1);
    		}
    	}
    }


    Here is the contents of the App1.config file:

    <?xml version="1.0" encoding="utf-8" ?>
    	<appSettings>
    		<add key="S1" value="Value of S1"/>
    	</appSettings>

    When I run the application I discover that the S1 key does not appear to be in Config.AppSettings.Settings. (I have tried wrapping the <appSettings> element in the App1.config file with a <Configuration> </Configuration> pair, but that made no difference.

    I do not think that it makes any difference, but here is the main application configuration file for completeness:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                <section name="ConsoleAppWithSettings.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
            </sectionGroup>
        </configSections>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
        </startup>
        <applicationSettings>
            <ConsoleAppWithSettings.Properties.Settings>
                <setting name="AppSpecificSetting" serializeAs="String">
                    <value>My application specific setting</value>
                </setting>
            </ConsoleAppWithSettings.Properties.Settings>
        </applicationSettings>
    </configuration>

    I suspect that I am missing something simple and obvious. Can someone please help me to see what I need to do to be able to get access to the "S1" setting in the main code.

    I am using Visual Studio 2015, if that is significant.

    (I have a larger question about the best way to handle common application settings, but I will leave that to another post).

    Thanks in advance.

    Andrew.


    • Edited by Andrew Ch. _ Friday, May 18, 2018 11:46 AM Add note about Visual Studio version
    Friday, May 18, 2018 11:45 AM

Answers

All replies

  • Hi Andrew,

    Try accessing app settings value as below. Hope this will help you.

    string DropDirectory = System.Configuration.ConfigurationManager.AppSettings["S1"];
    The above line of code will return the value associated with the key.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Friday, May 18, 2018 12:06 PM
    Moderator
  • App.configs only work in Debug mode at the project level. .NET looks for configuration information at the root project, like a project that makes the progranem.exe file is considered the root project. All configuration information must be placed in the app.config of the root project. Upon the successful build of the solution, Visual Stuido makes a runtime config file called programname.exe.config that has all the config info that in the app.config in the root project. And that's the config file that .NET looks for all configuration information to be in,  the runtime config file.

    You deploy the programname.exe file to a machine, then the programname.exe.config file must accompany the programname.exe file and be in the same location so that .NET can find the runtime config.

    Reading or writing of config data to the config file at runtime is happening with the runtime config file, which is sitting in the Bin folder with the programname.exe.

    Friday, May 18, 2018 12:10 PM
  • Hello Sabah,

    Thank you very much for taking the time to try and help me. I tried you suggestion, but the string returned is null.

    I think that my problem may be to do with the contents of the App1.config file -- maybe I have not structured the xml correctly?

    Andrew.

    Friday, May 18, 2018 12:21 PM
  • Should be something like this. Aren't you using the app.config file that get created automatically when you create a project?

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
      </configSections>
      <appSettings>
        <add key="S1" value="Value of S1" />    
      </appSettings>  
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
      </startup>
      <system.web>
        <membership defaultProvider="ClientAuthenticationMembershipProvider">
          <providers>
            <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
          </providers>
        </membership>
        <roleManager defaultProvider="ClientRoleProvider" enabled="true">
          <providers>
            <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
          </providers>
        </roleManager>
      </system.web>
    </configuration>


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Friday, May 18, 2018 3:00 PM
    Moderator
  • Hello DA924x,

    Thank you for taking the time to help. However, I think you have not quite got the idea of what I am trying to do. The file I am talking about is "App1.config" and NOT the main configuration file that Visual Studio creates ("app.config") and copes as APPNAME.exe.config into the bin folder. I have that APPNAME.exe.config file present in my bin folder AND the "App1.config" file.

    The main config file gets loaded automatically, and I am trying to load the "App1.config" as an additional configuration file.

    I hope that clears up the matter.

    Andrew.

    Friday, May 18, 2018 3:16 PM
  • Hello Sabah,

    I am out of the office now, I will try the configuration that you suggest when I am back in the office on Monday.

    I think I need to clarify a bit. I am using TWO config files (as shown in the original post). The "app.config" that gets automatically generated and copied to "<application_name>.exe.config" is present in the bin folder. That is the longer of the two config files shown. That is working fine (and represents the application specific configuration that I want).

    The second config file I have ("App1.config") is the one that I have trouble using. (This is the one that I want to use as a shared configuration file across a number of applications).

    Thanks again.

    Andrew.

    Friday, May 18, 2018 3:23 PM
  • Hello DA924x,

    Thank you for taking the time to help. However, I think you have not quite got the idea of what I am trying to do. The file I am talking about is "App1.config" and NOT the main configuration file that Visual Studio creates ("app.config") and copes as APPNAME.exe.config into the bin folder. I have that APPNAME.exe.config file present in my bin folder AND the "App1.config" file.

    The main config file gets loaded automatically, and I am trying to load the "App1.config" as an additional configuration file.

    I hope that clears up the matter.

    Andrew.

    The file is just XML this app1.config so you can use Linq-2-XML to read and manipulate the file.

    http://www.dotnetcurry.com/linq/564/linq-to-xml-tutorials-examples

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/linq-to-xml-overview

    Friday, May 18, 2018 6:53 PM
  • Andrew,

    So far what I have found is that .NET only loads one config file for the whole application. You cannot use multiple configuration files (i.e. one per library project) without coding. One solution you can try is use two app settings into the app.config file. Look into the below MSDN thread for more information. Hope this will helps you.

    2 app.config files in solution C#

    How to share app.config or web.config to many projects


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Sunday, May 20, 2018 3:46 PM
    Moderator
  • Hello Sabah,

    Just to keep you up to date... I have tried using the example configuration file as you suggested, unfortunately still no joy. I will keep investigating.

    Andrew.

    Monday, May 21, 2018 10:16 AM
  • Hi Andrew,

    Did you look into the articles I referenced above?

    Also, see the below article, it might have the solution that you are looking for. Also check the app.config properties to change the "Copy to output directory" option. Hope this will help you.

    splitting appsettings section of app.config in two parts


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, May 21, 2018 2:45 PM
    Moderator
  • Hello Sabah,

    Yes, thanks. I have got an approach working using configSource in the end. However, the second of the two articles looks like it may also be helpful. Thanks for your assistance.

    Andrew.

    Monday, May 21, 2018 3:02 PM
  • Hi Andrew,

    I glad to know that resolve the issueand thanks for sharing solution to us, please mark it as answer, it will be beneficial to other communities who have the similar issue.


    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 22, 2018 12:53 AM
    Moderator