C# 从 .NET 中的 app.config 或 web.config 读取设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1189364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:23:34  来源:igfitidea点击:

Reading settings from app.config or web.config in .NET

c#.netconfigurationappsettings

提问by Russ Clark

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

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

I've found that

我发现

ConfigurationSettings.AppSettings.Get("MySetting")

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

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

I've read that I should be using:

我读过我应该使用:

ConfigurationManager.AppSettings["MySetting"]

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

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

What is the best way to do this?

做这个的最好方式是什么?

采纳答案by womp

You'll need to add a referenceto System.Configurationin your project's references folder.

你需要一个引用添加System.Configuration您的项目的引用文件夹

You should definitely be using the ConfigurationManagerover the obsolete ConfigurationSettings.

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

回答by Otávio Décio

You must add a reference to the System.Configuration assembly to the project.

您必须向项目添加对 System.Configuration 程序集的引用。

回答by Shiva

Right click on your class library, and choose the "Add References" option from the Menu.

右键单击您的类库,然后从菜单中选择“添加引用”选项。

And from the .NET tab, select System.Configuration. This would include the System.Configuration DLL file into your project.

然后从 .NET 选项卡中,选择 System.Configuration。这会将 System.Configuration DLL 文件包含到您的项目中。

回答by Pagey

I'm using this, and it works well for me:

我正在使用它,它对我很有效:

textBox1.Text = ConfigurationManager.AppSettings["Name"];

回答by Wavare Santosh

web.configis used with web applications. web.configby default has several configurations required for the web application. You can have a web.configfor each folder under your web application.

web.config与 Web 应用程序一起使用。web.config默认情况下,Web 应用程序需要几个配置。您可以web.config在 Web 应用程序下为每个文件夹创建一个。

app.configis used for Windows applications. When you build the application in Visual Studio, it will be automatically renamed to <appname>.exe.configand this file has to be delivered along with your application.

app.config用于 Windows 应用程序。当您在 Visual Studio 中构建应用程序时,它会自动重命名为,<appname>.exe.config并且此文件必须与您的应用程序一起交付。

You can use the same method to call the app settingsvalues from both configuration files: System.Configuration.ConfigurationSettings.AppSettings["Key"]

您可以使用相同的方法app settings从两个配置文件中调用值: System.Configuration.ConfigurationSettings.AppSettings["Key"]

回答by Ram

For a sample app.config file like below:

对于如下所示的示例 app.config 文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="countoffiles" value="7" />
    <add key="logfilelocation" value="abc.txt" />
  </appSettings>
</configuration>

You read the above application settings using the code shown below:

您可以使用下面显示的代码阅读上述应用程序设置:

using System.Configuration;

You may also need to also add a reference to System.Configurationin your project if there isn't one already. You can then access the values like so:

如果还没有,您可能还需要System.Configuration在您的项目中添加一个引用。然后,您可以像这样访问值:

string configvalue1 = ConfigurationManager.AppSettings["countoffiles"];
string configvalue2 = ConfigurationManager.AppSettings["logfilelocation"];

回答by Custodio

I strongly recommend you to create a wrapperfor this call. Something like a ConfigurationReaderServiceand use dependency injectionto get this class. This way you will be able to isolate this configuration files for test purposes.

我强烈建议您为此调用创建一个包装器。类似于 aConfigurationReaderService并使用依赖注入来获取此类。通过这种方式,您将能够隔离此配置文件以进行测试。

So use the ConfigurationManager.AppSettings["something"];suggested and return this value. With this method you can create some kind of default return if there isn't any key available in the .config file.

所以使用ConfigurationManager.AppSettings["something"];建议并返回这个值。如果 .config 文件中没有任何可用的密钥,则使用此方法您可以创建某种默认返回值。

回答by Jason

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

几天来,我一直试图找到解决同一问题的方法。我能够通过在web.config文件的 appsettings 标记中添加一个键来解决这个问题。使用帮助程序时,这应该覆盖 .dll 文件。

<configuration>
    <appSettings>
        <add key="loginUrl" value="~/RedirectValue.cshtml" />
        <add key="autoFormsAuthentication" value="false"/>
    </appSettings>
</configuration>

回答by Victor Naranjo

You might be adding the App.config file to a DLL file. App.Config works only for executable projects, since all the DLL files take the configuration from the configuration file for the EXE file being executed.

您可能要将 App.config 文件添加到 DLL 文件中。App.Config 仅适用于可执行项目,因为所有 DLL 文件都从正在执行的 EXE 文件的配置文件中获取配置。

Let's say you have two projects in your solution:

假设您的解决方案中有两个项目:

  • SomeDll
  • SomeExe
  • 某个DLL
  • 某个执行器

Your problem might be related to the fact that you're including the app.config file to SomeDLL and not SomeExe. SomeDll is able to read the configuration from the SomeExe project.

您的问题可能与您将 app.config 文件包含到 SomeDLL 而不是 SomeExe 的事实有关。SomeDll 能够从 SomeExe 项目中读取配置。

回答by Tom

I had the same problem. Just read them this way: System.Configuration.ConfigurationSettings.AppSettings["MySetting"]

我有同样的问题。只需这样阅读: System.Configuration.ConfigurationSettings.AppSettings["MySetting"]