Reading settings from app.config or web.config in

2019-01-03 01:03发布

I'm working on a C# class library that needs to be able to read settings from the web.config or app.config file (depending on whether the DLL is referenced from an ASP.NET web application or a Windows Forms application).

I've found that

ConfigurationSettings.AppSettings.Get("MySetting")

works, but that code has been marked as deprecated by Microsoft.

I've read that I should be using:

ConfigurationManager.AppSettings["MySetting"]

However, the System.Configuration.ConfigurationManager class doesn't seem to be available from a C# Class Library project.

Does anyone know what the best way to do this is?

20条回答
欢心
2楼-- · 2019-01-03 01:21

web.config is used with web applications.web.config by default has several configurations required for the web application. You can have a web.config for each folder under your web application.

app.config is used for windows applications. When you build the application in vs.net, it will be automatically renamed to <appname>.exe.config and this file has to be delivered along with your application.

You can use the same method to call the app settings values from both config files :

System.Configuration.ConfigurationSettings.AppSettings["Key"]
查看更多
神经病院院长
3楼-- · 2019-01-03 01:22

for ex:

<applicationSettings>
        <MyApp.My.MySettings>
            <setting name="Printer" serializeAs="String">
                <value>1234 </value>
            </setting>
        </MyApp.My.MySettings>
    </applicationSettings>
Dim strPrinterName as string= My.settings.Printer
查看更多
唯我独甜
4楼-- · 2019-01-03 01:25

I had the same problem, just read them this way:

System.Configuration.ConfigurationSettings.AppSettings["MySetting"]
查看更多
淡お忘
5楼-- · 2019-01-03 01:26

You'll need to add a reference to System.Configuration in your project's references folder.

You should definitely be using the ConfigurationManager over the obsolete ConfigurationSettings.

查看更多
迷人小祖宗
6楼-- · 2019-01-03 01:28

As i found best approach to access app settings variables in systematic way by making a wrapper class over System.Configuration as below

public class BaseConfiguration
    {
        protected static object GetAppSetting(Type expectedType, string key)
        {
            string value = ConfigurationManager.AppSettings.Get(key);
            try
            {
                if (expectedType == typeof(int))
                    return int.Parse(value);
                if (expectedType == typeof(string))
                    return value;

                throw new Exception("Type not supported.");
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Config key:{0} was expected to be of type {1} but was not.",
                    key, expectedType), ex);
            }
        }
    }

Now we can access needed settings variables by hard coded names using another class as below

public class ConfigurationSettings:BaseConfiguration
    {
        #region App setting

        public static string ApplicationName
        {
            get { return (string)GetAppSetting(typeof(string), "ApplicationName"); }
        }

        public static string MailBccAddress
        {
            get { return (string)GetAppSetting(typeof(string), "MailBccAddress"); }
        }

        public static string DefaultConnection
        {
            get { return (string)GetAppSetting(typeof(string), "DefaultConnection"); }
        }

        #endregion App setting

        #region global setting



        #endregion global setting
    }
查看更多
在下西门庆
7楼-- · 2019-01-03 01:30

I have been trying to find a fix for this same issue for a couple days now. I was able to resolve this by adding a key within the appsettings tag in the web.config. This should override the .dll when using the helper.

<configuration>
<appSettings>
<add key="loginUrl" value="~/RedirectValue.cshtml" />
<add key="autoFormsAuthentication" value="false"/>
</appSettings>
</configuration>
查看更多
登录 后发表回答