我正在访问我的程序集配置:
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config";
Configuration conf = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
AppSettingsSection appSettings = conf.AppSettings;
我的.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>
如果编辑.config文件,我可以成功读取连接字符串,包括更改。所以,我知道我已连接到正确的文件。但我无法在appSettings对象中找到该字符串集合。它不在.Settings KeyValueConfigurationCollection中。我在哪里可以找到我的字符串集合?
答案 0 :(得分:6)
您应该使用这种更简单的语法
访问集合中的项目foreach (string s in CsDll.Properties.Settings.Default.StpInsertSearchPath)
{
Console.WriteLine(s);
}
编辑:
以下代码应该可以解决问题
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config";
Configuration conf = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
ConfigurationSectionGroup appSettingsGroup = conf.GetSectionGroup("applicationSettings");
ClientSettingsSection clientSettings = (ClientSettingsSection) appSettingsGroup.Sections["CsDll.Properties.Settings"];
ConfigurationElement element = clientSettings.Settings.Get("StpInsertSearchPath");
string xml = ((SettingElement)element).Value.ValueXml.InnerXml;
XmlSerializer xs = new XmlSerializer(typeof(string[]));
string[] strings = (string[])xs.Deserialize(new XmlTextReader(xml, XmlNodeType.Element, null));
foreach (string s in strings)
{
Console.WriteLine(s);
}
可能有一个较短的方式,但这对我有用。
答案 1 :(得分:2)
AppSettings和ConnectionStrings都是ConfigurationManager上可直接使用的属性。
然而, applicationSettings 和 userSettings ,与您在VS设置设计器中可以编辑的熟悉的设置。设置相对应,并非如此很容易得到。 AppSettings 不与 applicationSettings 相同, applicationSettings 位于正在使用的配置文件的完全不同的部分。
您必须使用上面的挖掘方法或变体来获取 applicationSettings 和 userSettings 。此外, applicationSettings 只会在您下次启动应用程序时更新,如果您甚至可以在运行时完全写入它们。
例如(从别处抄袭 - 谢谢):
public static string ReadSetting(string sectionGroupName, string sectionName, string settingName, Configuration config = null)
{
if (config == null)
config = SharedConfigSettings;
// Get sectionGroup
var sectionGroup =
config.GetSectionGroup(sectionGroupName);
// Get section
var section =
(ClientSettingsSection)sectionGroup.Sections.Get(sectionName);
// Get setting
var setting = section.Settings.Get(settingName);
// Read setting value
return setting.Value.ValueXml.InnerText;
}
和另一个例子(从许多例子中得到了答案 - 感谢世界):
///<summary>
/// return the applicationSettings section
///</summary>
///<returns></returns>
public static ClientSettingsSection GetSettingsSection(ConfigurationSectionGroup group, string clientSectionName)
{
return (ClientSettingsSection)group.Sections[clientSectionName];
}
///<summary>
/// return the section settings collection
///</summary>
///<returns></returns>
public static System.Configuration.SettingElementCollection GetSettingsCollection(ClientSettingsSection section)
{
return section.Settings;
}
///<summary>
/// return the connectionStrings section collection
///</summary>
///<returns></returns>
public static System.Configuration.SettingElementCollection ConnectionStringsCollection()
{
return ((ClientSettingsSection)SharedConfigSettings.GetSection("connectionStrings")).Settings;
}
///<summary>
/// A collection of all the UserSettings in a SettingElementCollection
///</summary>
///<returns></returns>
public static SettingElementCollection UserSettings()
{
return
GetSettingsCollection(GetSettingsSection(GetSettingsSectionGroup(@"userSettings"),
@"MyAssembly.Properties.Settings"));
}
///<summary>
/// A collection of all the ApplicationSettings in a SettingElementCollection
///</summary>
///<returns></returns>
public static SettingElementCollection ApplicationSettings()
{
return
GetSettingsCollection(GetSettingsSection(GetSettingsSectionGroup(@"applicationSettings"),
@"MyAssembly.Properties.Settings"));
}
然后,遗憾的是,您仍然需要处理这些部分中的设置集合中的SettingElement对象。每个都必须反序列化为属性Type,除非它是一个字符串,例如对于applicationSettings SettingElement(一个无法在运行时动态更新的):
(锻炼)
var y = GetSettingsSection(GetSettingsSectionGroup(@"applicationSettings"), @"MyAssembly.Properties.Settings");
var c = (y.Settings.Cast<SettingElement>().FirstOrDefault(s => s.Name == "WellKnownDirectories").Value).ValueXml
.InnerXml; // the setting as Xml
var xs = new XmlSerializer(typeof(string[]));
var strings = (string[])xs.Deserialize(new XmlTextReader(c, XmlNodeType.Element, null));
foreach (string s in strings)
{
Console.WriteLine(s);
}
对于字符串属性,它更容易(这个例子基本上是上面第一个的冗余):
var s = (y.Settings.Cast<SettingElement>().FirstOrDefault(s => s.Name == "MyUserSettingName").Value).ValueXml
.InnerText
所有这些示例都与applicationSettings混淆。同样的方法可以与userSettings一起使用,可能还有一些保存方法,等等,你需要跟踪(或多或少)几个配置文件中的哪些实际上正在运行 - 主要,漫游或本地
为什么我这样做?因为两个相关的应用程序和一个公共类库(或库)都需要使用其中一个应用程序所拥有的相同设置,其中设置是可视化管理的。有没有人以更好的方式解决这个问题?
感谢。
答案 2 :(得分:2)
如果是您正在尝试从设置中提取的StringCollection
var strings = (StringCollection) Properties.Settings.Default["StpInsertSearchPath"];
将在不需要XmlSerializer
的情况下完成任务答案 3 :(得分:1)
您的连接字符串通常位于配置管理器的ConnectionStrings属性中。您应该能够通过静态方法以更简单的方式访问。
string myConnectionString = ConfigurationManager.ConnectionStrings["connectioStringName"];
我相信您应该在.config文件中使用标记“AppSettings”而不是“ApplicationSettings”来启用ConfigurationManager通过AppSettings属性进行访问。
我不太了解ConfigurationManager如何确保这将解决您的问题,但重命名并删除该自定义部分组应该允许AppSettings正常工作。
修改的 是的,看起来ConfigurationManager的AppSettings属性访问了名为的部分 在.config文件中。