winform 程序中读写app.config

来源:互联网 发布:java数据库编程实例 编辑:程序博客网 时间:2024/06/05 19:45

在form项目中新建一个app.config文件


<?xml version="1.0" encoding="utf-8" ?>

<configuration>
  <appSettings>
    <add key="ConnectionString" value="Data Source = 。;user id = sa;password = 123456;Initial Catalog = bestoas" />
  </appSettings>

</configuration>


Model层

       public ConnectionStringInfo() { }

       private string _dataBaseIP;
       private string _dataBaseName;
       private string _dataBaseUid;
       private string _dataBasePwd;

       public string DataBaseIP {get;  set;}
       public string DataBaseName { get; set; }
       public string DataBaseUid { get; set; }
       public string DataBasePwd { get; set; }


 主要思路是修改xml文件  ,代码放到了DbHelperSQL中


添加引用:System.Windows.Forms;

      /// <summary>  
        /// 设置配置文件 对指定项设置指定值  
        /// </summary>  
        /// <param name="AppKey"></param>  
        /// <param name="AppValue"></param>  
        public static bool SetValue(string AppKey, string AppValue)
        {
            bool IsSuccessful = false;
            try
            {
    
                System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
                xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");


                System.Xml.XmlNode xNode;
                System.Xml.XmlElement xElem1;
                System.Xml.XmlElement xElem2;
                xNode = xDoc.SelectSingleNode("//appSettings");

                xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
                if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
                else
                {
                    xElem2 = xDoc.CreateElement("add");
                    xElem2.SetAttribute("key", AppKey);
                    xElem2.SetAttribute("value", AppValue);
                    xNode.AppendChild(xElem2);
                }
                xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
                IsSuccessful = true;
                return IsSuccessful;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                throw ex;
               
                
            }
            return IsSuccessful;
            
        } 


DAL层

     public bool UpdateConfigFile(ConnectionStringInfo connstrinfo)
       {
           string connectionStringValue = "";
           connectionStringValue = String.Format("Data Source = '{0}';user id = '{1}';password = '{2}';Initial Catalog = '{3}'",connstrinfo.DataBaseIP,connstrinfo.DataBaseUid,connstrinfo.DataBasePwd,connstrinfo.DataBaseName);

           bool stauts= DbHelperSQL.SetValue("ConnectionString",connectionStringValue);

           return stauts;
       }


UI层

            ConnectionStringInfo connectionstringinfo = new ConnectionStringInfo();
            connectionstringinfo.DataBaseIP = txtDataBaseIP.Text;
            connectionstringinfo.DataBaseName = txtDataBaseName.Text;
            connectionstringinfo.DataBasePwd = txtDataBaseUpwd.Text;
            connectionstringinfo.DataBaseUid = txtDataBaseUid.Text;

            bool stauts = new ConnectionStringManager().UpdateConfigFile(connectionstringinfo);



编译后会多三个文件

.vshost.exe

.vshost.exe.config

.vshost.exe.manifest


关于vshost.exe的解释:

http://baike.baidu.com/view/1587877.htm

  宿主进程是 Visual Studio 2005/2008/2010 中的一项功能,能提高调试性能,支持部分信任调试并支持设计时表达式计算。宿主进程文件的文件名中包含 vshost,并位于项目的输出文件夹中。

  ●提高的调试性能  宿主进程创建一个应用程序域并将调试器与应用程序关联起来。执行这些任务会导致在开始调试和开始运行应用程序之间有很明显的延迟。通过在后台创建应用程序域和关联调试器,并在运行应用程序之间保存应用程序域和调试器状态,宿主进程使性能得到提高。

  ●部分信任调试  在“项目设计器”的安全页中,可将应用程序指定为部分信任应用程序。调试部分信任应用程序需要对应用程序域进行特殊的初始化。此初始化由宿主进程处理。

  ●设计时表达式计算  设计时表达式计算使您可以从“即时”窗口进行代码测试,而不必运行应用程序。宿主进程在设计时表达式计算期间执行此代码。

注意

  宿主进程文件 (.vshost.exe) 由 Visual Studio 2005 使用,不应通过应用程序直接运行或部署。


启用调试后,修改的数据保存到了.vshost.exe.config中了。所以实际部署的时候,去掉下面的三个文件就可以了

.vshost.exe

.vshost.exe.config

.vshost.exe.manifest


去掉之后,修改数据库连接保存到了app.config这个文件中。







原创粉丝点击