【发布时间】:2016-03-22 04:46:29
【问题描述】:
我们有一个包含 exe.config 文件的应用程序,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="ConsoleSettings">
<section name="Console" type="MyApp.ConfigSection" allowLocation="true" allowDefinition="Everywhere" />
</sectionGroup>
</configSections>
<ConsoleSettings>
<Console Username="User" Password="user" LanAddress="192.168.42.11" Port="8792" />
</ConsoleSettings>
....
我想做的是读取文件,将 LanAddress 更改为用户输入的内容(例如,string newLanAddress),然后将其保存回来。
到目前为止,我有这个:
var configFile = new ExeConfigurationFileMap();
var configFile.ExeConfigFilename = "MyApp.exe.config";
var config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
var configGroup = config.SectionGroups[@"ConsoleSettings"];
var consoleSection = configGroup.Sections[0];
var lanAddress = consoleSection.// this is where I get stuck
如何访问 consoleSection 的 LanAddress 元素??
【问题讨论】:
-
你不能用
System.Configuration.ConfigurationManager.AppSettings[key];获得价值