我正在编写一个c#类库,需要能够从web读取设置。config或app.config文件(取决于DLL是否从ASP. config引用。NET web应用程序或Windows窗体应用程序)。

我发现

ConfigurationSettings.AppSettings.Get("MySetting")

但该代码已被微软标记为弃用。

我读到我应该使用:

ConfigurationManager.AppSettings["MySetting"]

但是,在c#类库项目中,System.Configuration.ConfigurationManager类似乎不可用。

最好的方法是什么?


当前回答

我也有同样的问题。你可以这样读: System.Configuration.ConfigurationSettings.AppSettings(“MySetting”)

其他回答

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

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

额外:如果你正在处理一个类库项目,你必须嵌入设置。json文件。

类库不应该直接引用 App.config类没有App.config,因为它不是一个 Application是一个类。

转到JSON文件的属性。 Change Build Action ->嵌入式资源。 使用下面的代码来阅读它。

var assembly = assembly . getexecutingassembly ();

var resourceStream = assembly。GetManifestResourceStream(“Assembly.file.json”);

string myString = reader.ReadToEnd();

现在我们有一个JSON字符串,我们可以使用JsonConvert反序列化它

如果您没有在程序集中嵌入该文件,则不能只使用DLL文件而不使用该文件

我可以在。net Core项目中使用以下方法:

步骤:

Create an appsettings.json (format given below) in your project. Next create a configuration class. The format is provided below. I have created a Login() method to show the usage of the Configuration Class. Create appsettings.json in your project with content: { "Environments": { "QA": { "Url": "somevalue", "Username": "someuser", "Password": "somepwd" }, "BrowserConfig": { "Browser": "Chrome", "Headless": "true" }, "EnvironmentSelected": { "Environment": "QA" } } public static class Configuration { private static IConfiguration _configuration; static Configuration() { var builder = new ConfigurationBuilder() .AddJsonFile($"appsettings.json"); _configuration = builder.Build(); } public static Browser GetBrowser() { if (_configuration.GetSection("BrowserConfig:Browser").Value == "Firefox") { return Browser.Firefox; } if (_configuration.GetSection("BrowserConfig:Browser").Value == "Edge") { return Browser.Edge; } if (_configuration.GetSection("BrowserConfig:Browser").Value == "IE") { return Browser.InternetExplorer; } return Browser.Chrome; } public static bool IsHeadless() { return _configuration.GetSection("BrowserConfig:Headless").Value == "true"; } public static string GetEnvironment() { return _configuration.GetSection("EnvironmentSelected")["Environment"]; } public static IConfigurationSection EnvironmentInfo() { var env = GetEnvironment(); return _configuration.GetSection($@"Environments:{env}"); } } public void Login() { var environment = Configuration.EnvironmentInfo(); Email.SendKeys(environment["username"]); Password.SendKeys(environment["password"]); WaitForElementToBeClickableAndClick(_driver, SignIn); }

你可以使用下面这条线。在我的例子中,它是有效的: System.Configuration.ConfigurationSettings.AppSettings(“yourKeyName”)

您必须注意上面的代码行也是旧版本,并且在新库中已弃用。

我使用的是Mac版本17.0.6的Visual Studio。

正如你在这张截图上看到的,不可能给System.Configuration添加引用。

解决方案:

安装NuGet Package - System.Configuration.ConfigurationManager 创建app.config文件,将“Build action”设置为“EmbeddedResource”

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="name" value="Joe"/>
    </appSettings>
</configuration>

使用System.Configuration; 享受)

string name = ConfigurationManager.AppSettings["name"];

BTW:不要为库添加app.config