怎样建立和程序中修改VB.NET应用程序的配置文件

我想将windows应用程序中的连接字符串保存到程序配置文件中,但是不知道怎样建立配置文件,请高手指点。
我做了如下尝试,结果失败:
建立XML文件为AppName.exe.config

1<configuration>
2<appsettings>
3<add key="constring" value="server=(local);database=Northwind;uid=sa;pwd=;"></add>
4</appsettings>
5</configuration>

将其保存到Bin目录下,但是调试结束后文件被自动删除
读取连接字符串语句如下:
Dim conn as new SqlConnection(ConfigurationSettings.Appsettings("constring"))
使用该连接时的错误提示:连接字符串未初始化。

请问:1、怎样正确建立应用程序配置文件
2、为什么调试的时候我所建立的文件会被删除
3、请高手给一个配置文件应用的实例代码

---------------------------------------------------------------

项目——>添加新项——>应用程序配置文件
---------------------------------------------------------------

主要是怎样通过代码更改配置文件中的内容

就像平时操作xml文件一下,可以用 XML 的那一套类库。比如把你的连接字符串改为 3456

Dim doc As New Xml.XmlDocument
doc.Load(System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
doc.SelectSingleNode("/configuration/appSettings/add[@key='constring']").Attributes("value").Value = "3456"
doc.Save(System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)

Published At
Categories with Web编程
Tagged with
comments powered by Disqus