我正在尝试完成此异常处理程序:
if (ConfigurationManager.ConnectionStrings["ConnectionString"]==null)
{
string pathOfActiveConfigFile = ...?
throw new ConfigurationErrorsException(
"You either forgot to set the connection string, or " +
"you're using a unit test framework that looks for "+
"the config file in strange places, update this file : "
+ pathOfActiveConfigFile);
}
Run Code Online (Sandbox Code Playgroud)
当我使用nUnit时,这个问题似乎只发生在我身上.
我无法访问配置文件中的值.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var clientsFilePath = config.AppSettings.Settings["ClientsFilePath"].Value;
// the second line gets a NullReferenceException
Run Code Online (Sandbox Code Playgroud)
.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- ... -->
<add key="ClientsFilePath" value="filepath"/>
<!-- ... -->
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
你有什么建议我该怎么办?
如何检查应用程序设置是否可用?
即app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key ="someKey" value="someValue"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
并在代码文件中
if (ConfigurationManager.AppSettings.ContainsKey("someKey"))
{
// Do Something
}else{
// Do Something Else
}
Run Code Online (Sandbox Code Playgroud) WebConfigurationManager和ConfigurationManager之间有什么区别?
我什么时候应该使用另一个?
更新
我只是查看了WebConfigurationManager,由于某种原因,您无法像在ConfigurationManager中那样访问连接字符串(如数组).谁能告诉我为什么MS这样做了?使用WebConfigurationManager获取所需的连接字符串似乎很痛苦.
再次更新CAVEAT!
如果您没有引用添加到项目中的"System.Configuration"命名空间,那么当您尝试访问WebConfigurationManager.ConnectionStrings时,Visual Studio将显示错误!
.net c# asp.net configurationmanager webconfigurationmanager
我在一个Java库,淘汰所有的硬编码值的过程中,不知道什么框架将是最好的(在零或近于零配置方面)来处理运行时配置?我更喜欢基于XML的配置文件,但它并不重要.
如果您对框架有实际经验,请仅回复.我不是在寻找例子,而是经验......
这可能听起来太琐事了,而且我做了与文章中建议的相同的事情,但它没有按预期工作.希望有人能指出我正确的方向.
我想保存每个AppSettings的用户设置.
一旦Winform关闭,我触发:
conf.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (ConfigurationManager.AppSettings["IntegrateWithPerforce"] != null)
ConfigurationManager.AppSettings["IntegrateWithPerforce"] =
e.Payload.IntegrateCheckBox.ToString();
else
config.AppSettings.Settings.Add("IntegrateWithPerforce",
e.Payload.IntegrateCheckBox.ToString());
config.Save(ConfigurationSaveMode.Modified);
Run Code Online (Sandbox Code Playgroud)
因此,当条目第一次不存在时,它只会创建它,否则它将修改现有条目.然而,这并不节省.
1)我做错了什么?
2)我希望在哪里再次保存应用程序设置的用户设置?是在Debug文件夹中还是在C:\ Documents and Settings\USERNAME\Local Settings\Application Data文件夹中?
我在我的Visual C#控制台应用程序(Visual Studio 2005 .NET 2.0 Framework)中包含以下语句
using System.Configuration;
Run Code Online (Sandbox Code Playgroud)
我在我的申请中使用以下声明:
ConfigurationManager.AppSettings["SomeStringOverHere"];
Run Code Online (Sandbox Code Playgroud)
我尝试构建应用程序并得到错误:当前上下文中不存在名称"ConfigurationManager".
有什么帮助吗?
我只是想知道ConfigurationManager.AppSettings [Key]是如何工作的?
每次需要密钥时它是否从物理文件中读取?
如果是这样,我应该在缓存中读取web.config的所有应用程序设置,然后从中读取吗?
或者ASP.NET或IIS只在application_startup加载web.config文件一次.
如何验证每次读取是否访问物理文件?
如果我更改了web.config,IIS将重新启动我的应用程序,因此无法以这种方式进行验证.
谢谢,
我正在使用C#,Framework 3.5(VS 2008).
我正在使用将ConfigurationManager配置(而不是默认的app.config文件)加载到Configuration对象中.
使用Configuration类,我能够得到一个ConfigurationSection,但我找不到获取该部分值的方法.
在配置中,ConfigurationSection是类型System.Configuration.NameValueSectionHandler.
对于什么是价值,当我使用的方法GetSection的ConfigurationManager(只能当它是对我的默认app.config文件),我收到了一个对象类型,我可以投进去对键值的集合,我刚刚收到像字典一样的价值.但是,当我ConfigurationSection从Configuration类接收类时,我无法进行此类转换.
编辑:配置文件的示例:
<configuration>
<configSections>
<section name="MyParams"
type="System.Configuration.NameValueSectionHandler" />
</configSections>
<MyParams>
<add key="FirstParam" value="One"/>
<add key="SecondParam" value="Two"/>
</MyParams>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我在app.config上使用它时的方式示例("GetSection"方法仅适用于默认的app.config):
NameValueCollection myParamsCollection =
(NameValueCollection)ConfigurationManager.GetSection("MyParams");
Console.WriteLine(myParamsCollection["FirstParam"]);
Console.WriteLine(myParamsCollection["SecondParam"]);
Run Code Online (Sandbox Code Playgroud) 使用C#.NET 3.5和WCF,我试图在客户端应用程序中写出一些WCF配置(客户端连接到的服务器的名称).
显而易见的方法是使用ConfigurationManager加载配置部分并写出我需要的数据.
var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel");
Run Code Online (Sandbox Code Playgroud)
似乎总是返回null.
var serviceModelSection = ConfigurationManager.GetSection("appSettings");
Run Code Online (Sandbox Code Playgroud)
完美的工作.
配置部分存在于App.config中,但由于某种原因ConfigurationManager拒绝加载该system.ServiceModel部分.
我想避免手动加载xxx.exe.config文件并使用XPath,但如果我不得不求助于我.看起来有点像黑客.
有什么建议?
c# ×9
.net ×5
appsettings ×3
asp.net ×2
xml ×2
app-config ×1
config ×1
frameworks ×1
java ×1
nunit ×1
settings ×1
wcf ×1
web-config ×1