我有以下问题:
我们有一个加载模块的应用程序(添加).这些模块可能需要app.config中的条目(例如WCF配置).因为模块是动态加载的,所以我不希望在我的应用程序的app.config文件中包含这些条目.
我想做的是以下内容:
注意:我不想覆盖默认的app.config!
它应该透明地工作,以便例如ConfigurationManager.AppSettings使用该新文件.
在我这个问题的评价,我用同样的解决方案提出了为这里提供:刷新的app.config与NUnit的.
不幸的是,它似乎没有做任何事情,因为我仍然从正常的app.config获取数据.
我用这段代码来测试它:
Console.WriteLine(ConfigurationManager.AppSettings["SettingA"]);
Console.WriteLine(Settings.Default.Setting);
var combinedConfig = string.Format(CONFIG2, CONFIG);
var tempFileName = Path.GetTempFileName();
using (var writer = new StreamWriter(tempFileName))
{
writer.Write(combinedConfig);
}
using(AppConfig.Change(tempFileName))
{
Console.WriteLine(ConfigurationManager.AppSettings["SettingA"]);
Console.WriteLine(Settings.Default.Setting);
}
Run Code Online (Sandbox Code Playgroud)
它打印相同的值twices,虽然combinedConfig包含除正常app.config之外的其他值.
我想通过3个不同的项目使用单个app.config.
如何访问配置?
ConfigurationManager.AppSettings["config1"]
Run Code Online (Sandbox Code Playgroud) OpenExeConfiguration有2个重载:
OpenMappedExeConfiguration只有1个原型:
似乎(2)和(3)都可用于打开特定的配置文件而不是默认的app.config文件.
那么它们之间的区别是什么?什么时候用哪个?
为什么我们在(1)和(2)中分隔UserLevel和Config File Location,但是将它们组合在(3)中?
谢谢你的回复.
我知道微软总是喜欢以多种方式做事.但它应该这样做是有原因的.在我的问题中,任何人都知道原因吗?我们需要赏金吗?)?
我是c#中配置部分的初学者,
我想在配置文件中创建自定义部分.我在谷歌搜索后尝试的是如下
配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="MyCustomSections">
<section name="CustomSection" type="CustomSectionTest.CustomSection,CustomSection"/>
</sectionGroup>
</configSections>
<MyCustomSections>
<CustomSection key="Default"/>
</MyCustomSections>
</configuration>
Run Code Online (Sandbox Code Playgroud)
CustomSection.cs
namespace CustomSectionTest
{
public class CustomSection : ConfigurationSection
{
[ConfigurationProperty("key", DefaultValue="Default", IsRequired = true)]
[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
public String Key
{
get { return this["key"].ToString(); }
set { this["key"] = value; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用此代码检索Section时,我收到一条错误说配置错误.
var cf = (CustomSection)System.Configuration.ConfigurationManager.GetSection("CustomSection");
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
谢谢.
编辑
我最终需要的是什么
<CustomConfigSettings>
<Setting id="1">
<add key="Name" value="N"/>
<add key="Type" …Run Code Online (Sandbox Code Playgroud) 我正在使用Console应用程序为一些半自动化测试编写测试平台,但我需要从我正在测试的项目中获取连接字符串.我不想直接引用其他应用程序,或者在我正在测试的项目中有一个访问器.
到目前为止我设法做的是在我的TestUtility项目中创建一个指向另一个项目的Web.config文件的链接,并将其设置为Copy if newer.它是我的测试项目的根文件夹中唯一的Web.config,但WebConfigurationManager.OpenWebConfiguration(null)似乎是打开一些OTHER Web.config,因为它中唯一的连接字符串是指.\ SQLEXPRESS(不在我的解决方案的任何文件中,我的路径将是.\sql2008在这个配置中 - 有所不同).
有关如何从另一个项目访问该配置部分的任何提示或提示?
(耶的第一个问题)
我正在编写与第三方应用程序互操作的应用程序.此应用程序通过DLL中的方法向开发人员公开API.前一段时间,这个应用程序的供应商开始将他们自己的.NET组件集成到他们的程序中,当他们这样做时,他们决定他们的组件应该ConfigurationManager在运行时使用它来获取设置.
这意味着什么:他们的程序,foo.exe调用fooengine.dll,以及从中读取其设置foo.exe.config.我的程序,bar.exe也调用fooengine.dll,它从中读取其设置bar.exe.config.
嗯,这是完全错误的.但是我该如何解决呢?
简单的解决方法是复制foo.exe.config的设置bar.exe.config.那会奏效,但这很愚蠢.这意味着从管理角度来看,必须在N个不同的文件中维护给定的设置.这迟早会失败.
我尝试在配置文件中configSource的appSettings部分放置一个属性.(碰巧,我正在使用该applicationSettings部分进行我的设置,他们正在使用他们的appSettings部分,所以我可以忍受只是从不同的文件中获取该部分.)但是ConfigurationManager不喜欢它:它想要路径configSource不仅相对于我的程序目录而且在我的程序目录之下.
我可以将他们的设置文件读取到一个XmlDocument然后自己设置.但是现在我将我的代码与他们的实现紧密结合起来; 如果他们推出了一个将设置移动到该applicationSettings部分的新版本(现在应该是2009年的版本),我的代码将会中断.
还有另一种出路吗?
我花了几个小时的时间偷偷看看C#框架类来解决这个问题.关于如何做到这一点有几个问题,但没有明确的答案.我能找到的最好的链接是C#DLL配置文件,它完全涵盖了我的用例,从全局.config文件读取.dll,无论哪个进程托管它.
您可以使用反射来创建处理程序对象,但它会降低可读性,这不是生产代码,只是一个片段来显示它的工作原理.我保持尽可能简单明了.
public class ConfigurationManager
{
public static object GetSection(string sectionName)
{
var configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(
new System.Configuration.ConfigurationFileMap(
@"X:\path\to\your\file.config"));
object retVal;
var section = configuration.GetSection(sectionName);
var xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(section.SectionInformation.GetRawXml());
if (section.SectionInformation.Type.Equals("System.Configuration.NameValueSectionHandler"))
{
var handler = new System.Configuration.NameValueSectionHandler();
retVal = handler.Create(null, null, xmlDoc.DocumentElement);
}
else if (section.SectionInformation.Type.Equals("System.Configuration.DictionarySectionHandler"))
{
var handler = new System.Configuration.DictionarySectionHandler();
retVal = handler.Create(null, null, xmlDoc.DocumentElement);
}
else
{
throw new System.Exception("Unknown type " + section.SectionInformation.Type);
}
return retVal;
}
}
Run Code Online (Sandbox Code Playgroud) 有没有更简单的方法来做下面的行?
var doc = new XmlDocument();
doc.Load("../../../MyWebSite/Web.config");
var mysqlconn = doc.DocumentElement.SelectSingleNode(
"//appSettings//add[@key='mysqlconn']").Attributes["value"].Value;
Run Code Online (Sandbox Code Playgroud) 我的单元测试工作,但在我添加一些代码以从web.config文件中的Custom Config部分获取值之后,单元测试停止工作.以下是我的代码,那些标有"// new"的行是我添加的新代码,它们打破了测试.
public partial class AccountController : Controller
{
private readonly string _twitterConsumerKey;
private readonly string _twitterConsumerSecret;
private readonly string _twitterAccessToken;
private readonly string _twitterAccessTokenSecret;
private readonly IUserService _userService;
public AccountController(IUserService userService)
{
if (userService == null)
throw new ArgumentNullException("userService");
_userService = userService;
_twitterConsumerKey = TwitterSettings.Settings.ConsumerKey; //new code
_twitterConsumerSecret = TwitterSettings.Settings.ConsumerSecret; //new code
_twitterAccessToken = TwitterSettings.Settings.AccessToken; //new code
_twitterAccessTokenSecret = TwitterSettings.Settings.AccessTokenSecret; //new code
}
public class TwitterSettings : ConfigurationSection
{
private static TwitterSettings settings = ConfigurationManager.GetSection("Twitter") as TwitterSettings;
public …Run Code Online (Sandbox Code Playgroud) 在:
使用ConfigurationManager从任意位置加载配置, 我发现了一种解决方案。我正在处理的项目使用网络上的appSettings.config文件位置。但是,当我尝试使用引用的代码时:
System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap("Z:\Settings\appSettings.config"); //Path to your config file
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
Run Code Online (Sandbox Code Playgroud)
到目前为止,一切都很好。appSettings.config文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Environment" value="Development" />
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
但是然后我到达以下行:
var settings = configuration.AppSettings.Settings;
Run Code Online (Sandbox Code Playgroud)
或使用它的任何东西(例如Settings.Count),都会收到无效的强制转换异常。基本上,我如何从中获得“环境”的价值?
我有一些应用程序的web.config文件.它位于一些随机位置.我必须解析这个web.config文件(获取所有键名和值).我尝试使用ConfigurationManager类来获取这些数据,但是当我尝试获取一些Sections(Configuration-> GetSection('section name'))时它会抛出异常.它会引发异常,因为我没有这个部分指向的dll(因为我只有web.config而不是整个应用程序).似乎GetSection方法检查底层的dll以获得更多信息,但我只需要值(dll的名称).
我能做什么,关闭这个机制,你知道其他简单的解决方案吗?