通过外部配置文件,我的意思是除web.config之外的.config文件.我已经看到了有关如何在运行时编辑web.config的所有示例,但我想编辑configSource为appSettings引用的配置文件.我想只修改外部文件,我将处理应用程序回收.
理想情况下,我想使用内置类来处理编辑,但如果唯一的选项是手动文件打开/解析等,那么sobeit.
所有这一切背后的一般想法是在应用启动时查看的设置页面,用户设置他们的详细信息然后保存更改,然后真正的应用程序启动.快速轻松地安装app/configure页面,所以我想尽可能利用.config.
谢谢!
FOLLOWUP - 使用XmlDocument更改appSetting键值的快速代码段:
string path = Server.MapPath("~/my.config");
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode node = doc.SelectSingleNode("/appSettings/add[@key='myKey']");
node.Attributes[1].Value = "myVal";
XmlTextWriter writer = new XmlTextWriter(path, null);
writer.Formatting = Formatting.Indented;
doc.WriteTo(writer);
writer.Flush();
writer.Close();
Run Code Online (Sandbox Code Playgroud)