日期:2014-05-18  浏览次数:20740 次

关于app.config文件读写
C# vs2005 做一个界面,显示一个表信息,并对其进行增删改查操作。我是通过“数据”,“添加数据源”,“使用设计器编辑数据集”。
然后直接把字段和表拉到Form中,自动绑定,完成设计界面。VS自动生成app.config,但是 XXXConnectionString 是连接字符串
public string XXXConnectionString {
  get {
  return ((string)(this["XXXConnectionString "]));
  }
只读的,没有Set,我不能通过其他地方动态设置config连接语句了。我该怎么办,才能对app.config进行设置,以及加密?

------解决方案--------------------
这已直接是修改connectionString的
private void SetValue(String AppKey, String AppValue)
{
XmlDocument xDoc=new XmlDocument();
xDoc.Load(GetAppName("file"));//配置文件名需修改
XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2;
xNode=xDoc.SelectSingleNode("//connectionStrings");
xElem1=(XmlElement)xNode.SelectSingleNode("//add[@name=\""+AppKey+"\"]");
if(xElem1!=null)
xElem1.SetAttribute("connectionString",AppValue);
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("name",AppKey);
xElem2.SetAttribute("connectionStrings",AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(GetAppName("file"));//配置文件名需修改
}
------解决方案--------------------
http://msdn.microsoft.com/zh-cn/library/system.configuration.configurationmanager.aspx

http://msdn.microsoft.com/zh-cn/library/system.configuration.configuration.aspx

用OpenExeConfiguration函数打开app.config,作为一个Configuration 类,操作挺方便的啊