5

I have a wpf application which is using a dll of a class library. I am unable to access the app.config value in the class library

How I'm trying to access app.config:

ConfigurationSettings.AppSettings["conStr"].ToString();

this is returning a null value using System.Configuration is also added .

Blue
  • 22,608
  • 7
  • 62
  • 92
VJL
  • 359
  • 1
  • 5
  • 10
  • Is there any reason not to expose the relevant properties through a public class defined by the library? – Jace Jul 24 '16 at 03:43

4 Answers4

1

Why don't you just put that value in the Main Project ?

If you're calling the class library from the Main Project. Then the code in the class library uses the AppConfig defined inside the main project.

It is a common practice to have the connection string in the AppConfig for the Main Project and not inside the Class Library.

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
0

I recomend to use your own class for save Class Library settings. Something like this:

public class GlobalSettings
{

    public double RedFiber { get; set; }
    public double GreenFiber { get; set; }
    public double BlueFiber { get; set; }
    public bool DeleteMinorViews { get; set; }


    public static GlobalSettings Load()
    {
        try
        {
            return JsonConvert.DeserializeObject<GlobalSettings>(File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\YOUR_APP_NAME\\"));
        }
        catch ()
        {
            return DefaultSettings;                
        }

    }

    public void Save()
    {
        File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\YOUR_APP_NAME\\", JsonConvert.SerializeObject(this, Formatting.Indented));
    }

    private static readonly GlobalSettings DefaultSettings = new GlobalSettings()
    {
        RedFiber = 0,
        GreenFiber = 255,
        BlueFiber = 0,
        DeleteMinorViews = true,
    };
}
streamdown
  • 390
  • 4
  • 17
0

I had the same issue. Try adding a settings file for your class library to store your values. The value will be stored in the app.config but accessed using the settings file.

This post explains better: Can a class library have an App.config file?

This post goes into more detail about settings files: Best practice to save application settings in a Windows Forms Application

If you prefer not to do the above you can also move your data to the main projects App.config file and it should work.

Community
  • 1
  • 1
Jake Matthews
  • 115
  • 2
  • 10
-3

try

ConfigurationManager.AppSettings["conStr"].ToString();

If the does not work post your App.Config

ConfigurationSettings.AppSettings is obsolete

paparazzo
  • 44,497
  • 23
  • 105
  • 176