我正在为我们正在开发的系统编写测试WinForms/C#/ .NET 3.5应用程序,我们不需要在运行时切换.config文件,但这变成了一场噩梦.
这是场景:WinForms应用程序旨在测试WebApp,分为5个子系统.测试过程适用于在子系统之间发送的消息,并且为了使该过程成功,每个子系统都有自己的.config文件.
对于我的测试应用程序,我写了5个单独的配置文 我希望我能够在运行时在这5个文件之间切换,但问题是:我可以编程方式编辑应用程序.config文件很多次,但这些更改只会生效一次.我一直在寻找一个表格来解决这个问题,但我仍然没有成功.
我知道问题定义可能有点令人困惑,但如果有人帮助我,我会非常感激.
提前致谢!
---更新01-06-10 ---
我之前没有提到过.最初,我们的系统是一个Web应用程序,每个子系统之间都有WCF调用.出于性能测试的原因(我们使用的是ANTS 4),我们必须创建程序集的本地副本并从测试项目中引用它们.听起来有点不对劲,但我们找不到令人满意的方法来衡量远程应用程序的性能.
---结束更新---
这是我正在做的事情:
public void UpdateAppSettings(string key, string value)
{
XmlDocument xmlDoc = XmlDocument.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
foreach (XmlElement item in xmlDoc.DocumentElement)
{
foreach (XmlNode node in item.ChildNodes)
{
if (node.Name == key)
{
node.Attributes[0].Value = value;
break;
}
}
}
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
System.Configuration.ConfigurationManager.RefreshSection("section/subSection");
}
Run Code Online (Sandbox Code Playgroud) c# configurationmanager app-config configuration-files .net-3.5
好的,所以......
<section name="test" type="System.Configuration.NameValueFileSectionHandler" />
<test>
<add key="foo" value="bar" />
</test>
var test = ConfigurationManager.GetSection("test");
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.调试器显示test包含一个键,foo.
但是GetSection回归object,所以我们需要一个演员:
var type = test.GetType();
// FullName: System.Configuration.ReadOnlyNameValueCollection
// Assembly: System
Run Code Online (Sandbox Code Playgroud)
好的,这应该很简单.所以....
using System;
var test = ConfigurationManager
.GetSection("test") as ReadOnlyNameValueCollection;
Run Code Online (Sandbox Code Playgroud)
错误!
The type or namespace ReadOnlyNameValueCollection does not exist in the namespace System.Configuration. Are you missing an assembly reference?
错... wtf?
一个演员来System.Collections.Specialized.NameValueCollection获取代码工作,但我真的不明白为什么错误.
ReadOnlyNameValueCollection在MSDN上搜索显示该类没有任何文档.它似乎不存在.然而,我的代码中有一个这种类型的实例.
我经常在我的解决方案的Configuration Manager中创建自定义构建配置.当我将先前创建的项目包含到解决方案中时,它们不会自动包含这些新配置.我发现使用适当的配置设置回填这些项目的唯一方法是手动编辑项目文件.
有没有办法强制解决方案中的所有项目都使用同一组Configuration Manager配置?
当我尝试使用时检索.config文件中的部分列表
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Run Code Online (Sandbox Code Playgroud)
config.Sections集合包含一堆系统部分,但我没有在configSections标记中定义文件的部分.
我试图从我的app.config读取设置,我确定它以前工作,但现在它返回nullReferenceException.
获取设置的代码如下:
codeValueUtilRx = ConfigurationManager.AppSettings["CODEVALUE_UTIL_RX"].Split(';').ToList();
Run Code Online (Sandbox Code Playgroud)
我的app-congfig如下:
<appSettings>
<add key ="LOGFILELOCATION" value ="C:\\RuleEditor\\"/>
<add key ="CODEVALUE_UTIL_RX" value="GCN;GRP;NDC;SPEC;TCC"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)
我感觉它看起来很明显,我只是想不通.我已经尝试将app-config移动到解决方案中的不同项目,并且我重新创建了文件,但没有运气.有任何想法吗?
我在项目中有很多项目.我得到的错误信息是这样的:
正在构建的项目的处理器体系结构"MSIL"与参考"Interop.Domino,Version = 1.2.0.0,Culture = neutral,processorArchitecture = x86","x86"的处理器体系结构之间存在不匹配.这种不匹配可能会导致运行时故障.请考虑通过Configuration Manager更改项目的目标处理器体系结构,以便在项目和引用之间调整处理器体系结构,或者依赖于具有与项目的目标处理器体系结构相匹配的处理器体系结构的引用.
我发现这个链接提供了一些有趣的信息.
但是,当我打开配置管理器时,一切看起来都很好:

知道我能做些什么来摆脱这个编译器消息?
我的目标是在控制台应用程序中Where对ConfigurationManager.ConnectionStrings集合使用LINQ 查询(假设添加了System.Configuration引用的新的.NET 4.5控制台应用程序,以及相应的using声明).
我开始用这个,这并没有工作:
var relevantSettings =
ConfigurationManager.ConnectionStrings.Where(s => s.Name.StartsWith("Xyz"));
Run Code Online (Sandbox Code Playgroud)
它告诉我:
方法'
IEnumerable<TSource> System.Linq.Enumerable.Where<TSource(this IEnumerable<TSource>, Func<TSource,bool>)' 的类型参数不能从用法中推断出来.尝试明确指定参数.
这抓住了我措手不及,所以我想这个要检查我的理智,但是这并没有工作之一:
foreach (var settingsin ConfigurationManager.ConnectionStrings)
{
if (settings.Name.StartsWith("Xyz")) Console.WriteLine("Found one!");
}
Run Code Online (Sandbox Code Playgroud)
它抱怨没有有关foreach声明,但对.Name位,出现错误:
无法解析符号'名称'
据我所知,有可能是一些防止编译器推断var的类型,仔细检查,我想这其中做的工作:
foreach (ConnectionStringSettings settings in ConfigurationManager.ConnectionStrings)
{
if (connectionString.Name.StartsWith("Xyz")) Console.WriteLine("Found one!");
}
Run Code Online (Sandbox Code Playgroud)
然而,除了解决我的直接问题外,这对我没什么帮助.我想了解这里发生了什么.
我想要做的就是使用一个简单的LINQ Where语句来获取app.config中连接字符串的子集.为什么编译器阻止我这样做?
我想定义一个自定义配置部分,并且有一个属性不会产生字符串,而是一个system.type(如果用户输入了一堆垃圾,则为null)
例如:
<myCustomConfig myAnnoyingType="System.String" />
Run Code Online (Sandbox Code Playgroud)
在C#中(在当前的现实世界中)
[ConfigurationProperty("myAnnoyingType")]
public string MyAnnoyingType
{
get { return (string)this["myAnnoyingType"]; }
}
// else where in the app
var stringType = thatConfig.MyAnnoyingType
var actualType = Type.GetType(stringType);
// wow that was boring.
Run Code Online (Sandbox Code Playgroud)
在C#中(在理想世界中)
[ConfigurationProperty("myAnnoyingType")]
public Type MyAnnoyingType
{
get { return (Type)this["myAnnoyingType"]; }
}
Run Code Online (Sandbox Code Playgroud)
我想要的不是必须将项目保存为C#中的字符串,然后将其转换为应用程序中的Type; 我希望这是作为ConfigurationManager职责的一部分自动完成的.
这可能吗?如果必须的话,我可以使用TypeConverter,只是将它保持为字符串似乎很弱,然后在应用程序中进行类型查找.这不难做到,当我知道我正在寻找一种类型时,似乎毫无意义,必须明确地做它的价值.
我想appSettings从一个名为的外部配置文件中读取我的控制台应用程序的一部分secrets.config,而其余的我希望从中读取app.config.
目前,我有这个设置,但它似乎没有读取secrets.config,它甚至没有告诉我有关读取失败.
在我的app.config中
<appSettings file = "secrets.config">
<add key = "Foo" value = "Bar" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
在secrets.config中,它与app.config位于同一文件夹中
<appSettings>
<add key = "Secret" value = "Tiger" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
在我的代码中
var secret = ConfigurationManager.AppSettings["Secret"];
// secret turns out to be null
Run Code Online (Sandbox Code Playgroud) .net c# configuration configurationmanager configuration-files
我正在使用AdventureWorks数据库进行项目,我正在尝试访问数据库,以便用户可以更改其帐户信息.该网站的用户是使用ASP.NET配置管理器设置的,所以我不确定如何去做.我有一个GETCUSTOMER选择方法设置为取(此时)刚刚在Person.Contact表的客户的名字和姓氏.
[DataObject(true)]
public static class CustomerDB
{
[DataObjectMethod(DataObjectMethodType.Select)]
public static List<Customer> GetCustomer()
{
List<Customer> CustomerList = new List<Customer>();
SqlConnection con = new SqlConnection(GetConnectionString());
string sel = "SELECT Person.Contact.FirstName, Person.Contact.LastName " +
"FROM Person.Contact " +
"JOIN Sales.Individual ON Person.Contact.ContactID = Sales.Individual.ContactID " +
"JOIN Sales.Customer ON Sales.Individual.CustomerID = Sales.Customer.CustomerID " +
"WHERE Sales.Customer.CustomerType = Sales.Individual " +
"ORDER BY Person.Contact.LastName, Person.Contact.FirstName";
SqlCommand cmd = new SqlCommand(sel, con);
con.Open();
SqlDataReader rdr =
cmd.ExecuteReader(CommandBehavior.CloseConnection);
Customer customer;
while (rdr.Read())
{
customer = new …Run Code Online (Sandbox Code Playgroud)