我正在访问我的程序集的配置,如下所示:
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config";
Configuration conf = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
AppSettingsSection appSettings = conf.AppSettings;
Run Code Online (Sandbox Code Playgroud)
我的.config文件包含这样的部分
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="CsDll.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings>
<add name="CsDll.Properties.Settings.SabreCAD" connectionString="A Connection string." />
<add name="CsDll.Properties.Settings.StpParts" connectionString="Another connection string" />
</connectionStrings>
<applicationSettings>
<CsDll.Properties.Settings>
<setting name="StpInsertSearchPath" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>A string</string>
<string>Another string in the collection</string>
Run Code Online (Sandbox Code Playgroud)
如果我编辑.config文件,我可以成功读取连接字符串,包括更改.所以,我知道我已连接到正确的文件.但我无法在appSettings对象中找到该字符串集合.它不在.Settings KeyValueConfigurationCollection中.我在哪里可以找到我的字符串集合?
我正在使用VS2008.我有一个项目连接数据库,连接字符串通过ConfigurationManager从App.config读取.我们正在使用L2E.
现在我添加了一个辅助项目AndeDataViewer,它有一个简单的UI来显示数据库中的数据以进行测试/验证.
我不想在帮助项目中创建另一组实体数据模型.我刚刚将所有相关文件添加为新助手项目中的链接.
编译时,我收到以下错误:
Error 15 The name 'ConfigurationManager' does not exist in the current context C:\workspace\SystemSoftware\SystemSoftware\src\systeminfo\RuntimeInfo.cs 24 40 AndeDataViewer
Run Code Online (Sandbox Code Playgroud)
我想我可能需要从主项目中添加另一个项目设置/配置相关文件的链接到帮助项目?新助手项目中没有App.config文件.但它看起来我无法添加该文件的链接到帮助项目.有任何想法吗?
现在,我打电话给以下一行
System.Configuration.Configuration cnf = ConfigurationManager.OpenMachineConfiguration();
Run Code Online (Sandbox Code Playgroud)
结果是以下cnf.FilePath == C:\ Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config
我在32位2003服务器和64位2008 R2服务器上得到以下结果.理想情况下,我想在64位服务器上安装时返回64位文件夹.
aka - C:\ Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config
有没有办法获得64位版本而不需要使用ConfigurationFileMaps - 例如 来自Msdn的配置示例
更新评论
获取当前配置文件的文件路径时,使用AppDomain命名空间和ConfigurationManager命名空间有什么区别?你什么时候用一个而不是另一个?
例如,两者都返回相同的路径:
string f1 = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
string f2 = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None ).FilePath;
Run Code Online (Sandbox Code Playgroud) 我正在尝试为Web服务背后的代码编写测试.此代码位于从服务本身调用的单独的类库中.此类库用于ConfigurationManager获取其连接字符串,通常在运行由Web服务填充的Web服务时web.config.但是,当我从测试代码中运行它时,它们会抛出null异常.
我已经绕过网络寻找答案了一段时间,并尝试了几个类似问题的修复,但没有任何效果.(没有什么是我的情况.)
当您使用应用程序的当前配置文件时,从使用System.Configuration.NameValueSectionHandler定义的部分的配置文件中获取值很容易.
示例配置文件.
<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)
示例代码,可以轻松读取它.
NameValueCollection myParamsCollection =
ConfigurationManager.GetSection("MyParams") as NameValueCollection;
Run Code Online (Sandbox Code Playgroud)
这是不起作用的代码.
NameValueCollection collection =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
.GetSection("MyParams") as NameValueCollection;
Run Code Online (Sandbox Code Playgroud)
在编译时失败并出现以下错误.
无法通过引用转换,装箱转换,拆箱转换,换行转换或空类型转换将类型'System.Configuration.ConfigurationSection'转换为'System.Collections.Specialized.NameValueCollection'.
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)返回System.Configuration.Configuration,Configuration.GetSection返回ConfigurationSection.
ConfigurationManager.GetSection返回对象.
那么,当我必须使用OpenExeConfiguration时,如何取回我的NameValueCollection?
我正在努力配合.NET 2.0中的配置和设置类
如果以下内容包含在名为的文件中 app.config
<config>
<appSettings>
<add key="Foo" value="Hello World!"/>
</appSettings>
</config>
Run Code Online (Sandbox Code Playgroud)
我知道我可以访问appSetting
// this returns "Hello World!"
ConfigurationManager.AppSettings["Foo"]
Run Code Online (Sandbox Code Playgroud)
但是,如果调用该文件app1.config(或任何其他名称),我无法访问appSetting.只要我明白,ConfigurationManager.OpenExeConfiguration我应该阅读自定义配置设置文件.
Configuration conf = ConfigurationManager.OpenExeConfiguration(@"..\..\app1.config");
// this prints an empty string.
Console.WriteLine(conf.AppSettings.Settings["Foo"]);
Run Code Online (Sandbox Code Playgroud)
但是conf.AppSettings.Settings["Foo"]返回一个空字符串.
我也试过以下代码但没有成功
ExeConfigurationFileMap exeFileMap = new ExeConfigurationFileMap();
exeFileMap.ExeConfigFilename = System.IO.Directory.GetCurrentDirectory()
+ "\\App1.config";
Configuration myConf = ConfigurationManager.OpenMappedExeConfiguration
(exeFileMap, ConfigurationUserLevel.None);
// returns empty string as well
Console.WriteLine(myConf.AppSettings.Settings["Foo"]);
Run Code Online (Sandbox Code Playgroud)
如何从不称为app.config的文件中读取设置?
我有一个项目需要为许多不同的客户进行测试和部署,而这些客户有时会有不同的需求.它是一个MVVC应用程序,我使用.net MVC,但只有少数部分和部分,如一些部分视图等,其他一切都是使用knockout.js的单页面应用程序.
所以我有不同的web.config文件,但我有一个包含客户端配置的文件,一个.js文件,对于每个客户都是不同的.
现在,如果使用配置管理器在visual studio中进行不同的配置,我会根据我选择的配置获得不同的web.config,这是完美的.
但是有没有办法在项目中为另一个文件获得相同的结果?
例如有一个myfile.nightly.js和myfile.debug.js等等.(这些文件包含配置设置,如页面颜色或徽标或要隐藏的部分,适用于每个不同的客户).
我在预建事件中尝试了这个解决方案:http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx
但这是一个非常有用的帖子,我想知道VS2012是否有更实际的内容
我遇到了想要写入.exe.config的应用程序的问题.
请参阅以下代码:
using System;
using System.Configuration;
using System.IO;
namespace ConfigurationManagerError
{
class Program
{
static void Main(string[] args)
{
try
{
// set Config-File to persistent folder
ExeConfigurationFileMap exeMap = new ExeConfigurationFileMap();
exeMap.ExeConfigFilename = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
"ConfigError\\ConfigurationManagerError.exe.config");
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeMap, ConfigurationUserLevel.None);
config.AppSettings.Settings.Add(
new KeyValueConfigurationElement("TestKey", "TestValue"));
config.Save(ConfigurationSaveMode.Modified);
Console.WriteLine("Successfully write of Configuration-File to " + config.FilePath);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.Read();
}
}
}
Run Code Online (Sandbox Code Playgroud)
只要我作为具有读写访问权限的用户运行该文件夹一切正常.如果我的用户在该文件夹中没有写入权限,则会出现一个异常,指出不允许写入.exe.config的权限.到目前为止,一切都如预期.
但是,如果我现在有一个用户有权写入现有文件而不是创建新文件,则抛出异常
Access to the path 'C:\ProgramData\ConfigError\ila2ba_0.tmp' is denied
Run Code Online (Sandbox Code Playgroud)
似乎ConfigurationManager想要创建一个tmp-File.怎么解决这个问题? …
我正在使用Visual Studio 2013与.NET Framework 4.5.
我已System.Configuration将该项目作为参考.
我还在类中包含了一个using语句:
using System.Configuration;
Run Code Online (Sandbox Code Playgroud)
但是,以下行仍然给出了上述主题错误.我如何摆脱错误?
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WarrantySqlConnection"].ToString()))
Run Code Online (Sandbox Code Playgroud)
编译时错误出现在ConfigurationManager单词上.