由于在客户端部署的时候,AppSetting配置的值要根据实际需要配置,让客户直接改,保存的时候xml文档,
会直接变成txt文档。那只好在增加一个配置功能。
在网上查了一下资料。根据前人的经验,已顺利实现这个功能。现总结如下:
首先获取AppSettingSection配置节,判断是否允许修改,允许的话,修改,并保持。
代码如下:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
AppSettingsSection configSection = config.AppSettings;
try
{
if (configSection != null)
{
if (configSection.IsReadOnly() == false && configSection.SectionInformation.IsLocked == false)
{
configSection.Settings[Util.IndexFolderPath].Value = selectedPath;
config.Save();
}
}
}
catch (ConfigurationException ex)
{
MessageBox.Show(ex.Message, "Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
参考:
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/813c7b30-9d18-4484-86a0-630b54c897f3/