我正在像这样访问程序集的配置:
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 中。在哪里可以找到我的弦乐收藏?
您应该使用这种更简单的语法来访问集合中的项目
foreach (string s in CsDll.Properties.Settings.Default.StpInsertSearchPath)
{
Console.WriteLine(s);
}
EDIT:
下面的代码应该可以解决问题
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);
}
可能有更短的方法,但这对我有用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)