多个WINFORM项目(多个EXE)共用一个APP.CONFIG

  点击每个项目的右键属性——生成——输出——输出路径,这个路径最好是在主项目下建一个活页夹(比如file),输出路径就选择这个路径。

  点击主项目的右键属性——发布——发布位置可选择自己指定的一个地方。安装模式和和设置选择第二项,点击“应用程序文件”按钮,列出应用程序列表,系统必备的就不管了,但是如果是编程人员手动加入引用的DLL文件、exe文件、exe.confige文件等必须把发布状态改为包括(自动)。点击“系统必备”按钮,选择系统必备的组件,有些是默认选中状态,如果选择的是.net FrameWork 2.0 选择安装目位置为第二项,即从应用程序相同位置下载,其它可以根据需要设置。

 

 

如下两个方法是设置和读取Confige

先要添加引用DllConfigDemo.dll

页面顶部需要些using System.Configuration;

 

读取confige:

        private void GetConfige ()

        {

            string t1 = "";

            string t2 = "";

            string t3 = "";

            string t4 = "";

            string configFileName = Application.StartupPath + "//SSM.exe.config";//请根据实际情况修改  app.confige发布后就成了.exe.config文件

            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

            doc.Load(configFileName);

 

            XmlNodeList nodes = doc.GetElementsByTagName("setting");

            foreach (XmlNode node in nodes)

            {

                XmlElement xe = (XmlElement)node;

                if (xe.GetAttribute("name") == "SSMConnect")

                {

                    XmlNodeList nodes1 = xe.GetElementsByTagName("value");

                    foreach (XmlNode node1 in nodes1)

                    {

                        XmlElement ve = (XmlElement)node1;

                        string txtstr = ve.InnerText;

                        if (txtstr.LastIndexOf(';') != -1)

                        {

                            string[] list = txtstr.Split(';');

 

                            for (int i = 0; i < list.Length; i++)

                            {

                                if (i == 0)

                                {

                                    t1=list [0];

                                }

                                else if (i == 1)

                                {

                                    t2=list [1];

                                }

                                else if (i == 2)

                                {

                                    t3 = list[2];

                                }

                                else

                                {

                                    t4 = list[3];

                                }

                            }

                                

                            if (t1.Length > 8)

                            {

                                txt1.Text = t1.Substring(t1.LastIndexOf ("=")+1);

                            }

                            if (t2.Length > 10)

                            {

                                txt2.Text = t2.Substring(t2.LastIndexOf("=") + 1);

                            }

                            if (t3.Length > 5)

                            {

                                txt3.Text = t3.Substring(t3.LastIndexOf("=") + 1);

                            }

 

                            if (t4.Length > 5)

                            {

                                txt4.Text = t4.Substring(t4.LastIndexOf("=") + 1);

                            }

                        }

                    }

                }

            }

      

     }

 

    读取confige:

    private void SetConfige ()

        {

            string t1 = txt1.Text.Trim();

            string t2 = txt2.Text.Trim();

            string t3 = txt3.Text.Trim();

            string t4 = txt4.Text.Trim();

            string constr = "Data Source=" + t1 + ";Initial Catalog=" + t2 + ";User ID=" + t3 + ";Password=" + t4 + "";

            string configFileName = Application.StartupPath + "//SSM.exe.config";//请根据实际情况修改

            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

            doc.Load(configFileName);

          

            XmlNodeList nodes = doc.GetElementsByTagName("setting");

            foreach (XmlNode node in nodes)

            {

                XmlElement xe = (XmlElement)node;

                if (xe.GetAttribute("name") == "SSMConnect")

                {

                    XmlNodeList nodes1 = xe.GetElementsByTagName("value");

                    foreach (XmlNode node1 in nodes1)

                    {

                        XmlElement ve = (XmlElement)node1;

                        ve.InnerText = constr;

                        // xe.SetAttribute("value", constr);

                    }

                }

            }

            doc.Save(configFileName);

            MessageBox.Show("设置成功!");

            this.Close();

    }

 

 

 

   程序中使用app.congfige

   public ConnectionDb()

    {

        string configFileName = "SSM.exe.config";//请根据实际情况修改

        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

        doc.Load(configFileName);

      

        XmlNodeList nodes = doc.GetElementsByTagName("setting");

        foreach (XmlNode node in nodes)

        {

            XmlElement xe = (XmlElement)node;

            if (xe.GetAttribute("name") == "SSMConnect")

            {

                XmlNodeList nodes1 = xe.GetElementsByTagName("value");

                foreach (XmlNode node1 in nodes1)

                {

                    XmlElement ve = (XmlElement)node1;

                    

                    string[] strConfig = ve.InnerText.Split(';');

                    conStr = strConfig[0].Replace("server=", "").Trim();

                    conStr = conStr + ";" + strConfig[1].Replace("database=", "").Trim();

                    conStr = conStr + ";" + strConfig[2].Replace("uid=", "").Trim();

                    conStr = conStr + ";" + strConfig[3].Replace("pwd=", "").Trim();

                }

            }

        }

  

    }

 

发布后再进行打包安装:

打包安装时添加输出路径中的活页夹(file)中每个项目的exe文件,以及主项目的.exe.cofige文件,还有就是添加手动添加的应用dll文件即可。

你可能感兴趣的:(Winform)