小编Stu*_*len的帖子

ASP.Net - 如何在运行时以编程方式编辑外部配置文件

通过外部配置文件,我的意思是除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)

c# asp.net

7
推荐指数
2
解决办法
3114
查看次数

标签 统计

asp.net ×1

c# ×1