从 .NET 中的 app.config 或 web.config 读取设置 [英] Reading settings from app.config or web.config in .NET

查看:41
本文介绍了从 .NET 中的 app.config 或 web.config 读取设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 C# 类库,它需要能够从 web.configapp.config 文件中读取设置(取决于 DLL是从 ASP.NET Web 应用程序或 Windows 窗体应用程序引用的).

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).

我发现了

ConfigurationSettings.AppSettings.Get("MySetting")

有效,但该代码已被 Microsoft 标记为已弃用.

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

我已经读到我应该使用:

I've read that I should be using:

ConfigurationManager.AppSettings["MySetting"]

但是,System.Configuration.ConfigurationManager 类似乎无法从 C# 类库项目中获得.

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

最好的方法是什么?

推荐答案

您需要在项目的引用中添加引用System.Configuration文件夹.

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

您绝对应该使用 ConfigurationManager 而不是过时的 ConfigurationSettings.

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

这篇关于从 .NET 中的 app.config 或 web.config 读取设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆