我正在编写一个c#类库,需要能够从web读取设置。config或app.config文件(取决于DLL是否从ASP. config引用。NET web应用程序或Windows窗体应用程序)。
我发现
ConfigurationSettings.AppSettings.Get("MySetting")
但该代码已被微软标记为弃用。
我读到我应该使用:
ConfigurationManager.AppSettings["MySetting"]
但是,在c#类库项目中,System.Configuration.ConfigurationManager类似乎不可用。
最好的方法是什么?
我使用的是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
此外,您可以使用Formo:
配置:
<appSettings>
<add key="RetryAttempts" value="5" />
<add key="ApplicationBuildDate" value="11/4/1999 6:23 AM" />
</appSettings>
代码:
dynamic config = new Configuration();
var retryAttempts1 = config.RetryAttempts; // Returns 5 as a string
var retryAttempts2 = config.RetryAttempts(10); // Returns 5 if found in config, else 10
var retryAttempts3 = config.RetryAttempts(userInput, 10); // Returns 5 if it exists in config, else userInput if not null, else 10
var appBuildDate = config.ApplicationBuildDate<DateTime>();
我在这个链接https://stackoverflow.com/a/1836938/1492229找到了答案
不仅需要使用名称空间System.Configuration。您还必须将引用添加到程序集System.Configuration.dll,通过
右键单击引用/依赖项
选择添加参考
找到并添加System.Configuration。
这肯定有用。
同样,对于NameValueCollection,你必须写:
using System.Collections.Specialized;
我可以在。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);
}