0

I've got a Solution which have 6 another projects, and I'm trying to acces the App.config from a non main project and get some properties, but it returns null. Not happy, I tried to get a property from the main project's App.config, and still null.

I've tried to use two ways to get the App.config properties:

var a = ConfigurationManager.AppSettings.Get("cdEntidadeSistemaIND");
var b = ConfigurationManager.AppSettings["cdEntidadeSistemaIND"];
  • 1
    Without seeing how you try to access the app.config and what the structure of your projects is like, it's doubtful that anyone will be able to help you. – germi Nov 06 '19 at 11:56
  • 1
    You have to place all your configuration properties in the executable (main) which you are running from. – eran otzap Nov 06 '19 at 11:58
  • Ok @eranotzap, but I've tried to access the App.config from my main project, and it keeps returning null – Gustavo Brandão Nov 06 '19 at 12:05
  • related : https://stackoverflow.com/questions/1189364/reading-settings-from-app-config-or-web-config-in-net – xdtTransform Nov 06 '19 at 12:16
  • `ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);`. is the config in the curernt path you are executing from? – xdtTransform Nov 06 '19 at 12:18
  • https://stackoverflow.com/a/1009227/9260725 – xdtTransform Nov 06 '19 at 12:21
  • The App.config is on a Class Library project, but if I try to get any property from main's App.config, I've got the same thing @xdtTransform – Gustavo Brandão Nov 06 '19 at 12:26
  • HUm, something must be missing. Can you provide an [mcve]? Just a main trying to read one config field and the config file. On a fresh project this is a no repro for now. – xdtTransform Nov 06 '19 at 12:28
  • What kind of solution you have ? Is your main project a Web aplication or web api or something else ? It will be helpful if you can give more deatils. – nzrytmn Nov 06 '19 at 12:55
  • well post code + xml for your app.config and let's see. – eran otzap Nov 06 '19 at 12:59

3 Answers3

0

You need to get this way

var a = ConfigurationManager.AppSettings["cdEntidadeSistemaIND"].ToString();

Make sure your app settings are defined like this

<appSettings>
  <add key="cdEntidadeSistemaIND" value="cdEntidadeSistemaINDValue" />
</appSettings>

Don't forget to add

 using System.Configuration;
Pankaj
  • 2,618
  • 3
  • 25
  • 47
0

Are you accessing the app.config settings from the main application, or are you in a referenced project and trying to access THAT project's app.config settings? If it is the latter, any attempts to access appSettings would be from the calling project's app.config, NOT from the referenced project's app.config.

In order to access the app.config settings from the referenced library, you will need to manually load that configuration file.

Ryan Peters
  • 7,608
  • 8
  • 41
  • 57
  • I'm in a referenced project and trying to access this one project's app.config settings. But i tried to access directly the main's app.config, and this returns null too – Gustavo Brandão Nov 06 '19 at 12:34
0

I don't know if it is a good practice, but works for me.

I get the value using the Properties.Settings.Default.PropName.ToString() statement.