Application Configuration in C # Class Library

I have a wpf application that uses class library dlls. I cannot access the app.config value in the class library

How am I trying to access app.config:

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

this returns a null value using System.Configuration.

+4
source share
4 answers

Why don't you just put this value in the main project?

If you call the class library from the main project. Then the code in the class library uses the AppConfig defined inside the main project.

Common practice is to have a connection string in AppConfig for the main project, and not inside the class library.

+1
source

. - :

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,
    };
}
0

. , . app.config, .

: App.config?

: Windows Forms

, App.config, .

0

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

, App.Config

Configuration Settings.AppSettings deprecated

-3
source

Source: https://habr.com/ru/post/1648881/


All Articles