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

我发现

ConfigurationSettings.AppSettings.Get("MySetting")

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

我读到我应该使用:

ConfigurationManager.AppSettings["MySetting"]

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

最好的方法是什么?


当前回答

额外:如果你正在处理一个类库项目,你必须嵌入设置。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文件而不使用该文件

其他回答

您需要向System添加一个引用。在项目的引用文件夹中的配置。

你绝对应该使用ConfigurationManager,而不是过时的ConfigurationSettings。

我可以在。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); }

我一直试图找到一个解决这个问题的几天了。我可以通过在web中的appsettings标签中添加一个键来解决这个问题。配置文件。这将在使用helper时覆盖.dll文件。

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

我使用的是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

请检查您正在使用的. net版本。它应该大于4。你必须添加系统。配置系统库到您的应用程序。