天天看點

網站安裝打包 webconfig修改[三]

在net中,在system.configuration.configurationmanager中,提供了幾個靜态方法,用來修改配置檔案。

如:system.configuration.configuration config = system.configuration.configurationmanager.openmachineconfiguration();

獲得應用程式下的配置檔案,之後再用config進行操作。

如果是在web中,那就是操作webconfig了!不過現在在winform中,就成了操作app.config了。

于是,我選擇了還是以操作xml的方式來修改webconfig。

這裡寫了幾個類,主要也是模仿config的操作方式。代碼如下:

網站安裝打包 webconfig修改[三]
網站安裝打包 webconfig修改[三]

using system;

using system.collections.generic;

using system.text;

using system.xml;

namespace iishelper

{

    public class webconfighelper : idisposable

    {

        private bool loadisok;

        /// <summary>

        /// 加載是否成功

        /// </summary>

        public bool loadisok

        {

            get { return loadisok; }

            set { loadisok = value; }

        }

        private xmldocument xdox = new xmldocument();

        private string configpath = string.empty;

        public webconfighelper(string webconfigpath)

            try

            {

                xdox.load(webconfigpath);

                configpath = webconfigpath;

                loadisok = true;

            }

            catch { loadisok = false; }

        public webconfigappsetting appsetting

            get

                xmlnode xnode=xdox.selectsinglenode("//configuration/appsettings");

                if(xnode==null)

                {

                    return null;

                }

                return new webconfigappsetting(ref xnode);

        public webconfigconnectionstrings connectionstrings

                xmlnode xnode = xdox.selectsinglenode("//configuration/connectionstrings");

                if (xnode == null)

                return new webconfigconnectionstrings(ref xnode);

        public bool save()

                xdox.save(configpath);

                return true;

            catch { }

            return false;

        #region idisposable 成員

        public void dispose()

            xdox = null;

        #endregion

    }

    public abstract class webconfigbase

        protected xmlnode node;

        public  void add(string key, string value){}

        public abstract void set(string key, string value);

        public abstract string get(string key);

        public  void remove(string key, string value){}

    public class webconfigappsetting : webconfigbase

        internal  webconfigappsetting(ref xmlnode xnode)

            node = xnode;

        public override void set(string key, string value)

            foreach (xmlnode addnode in node.childnodes)

                if (addnode.attributes != null && addnode.attributes["key"].value == key)

                    addnode.attributes["value"].value = value;

                    break;

        public override string get(string key)

                  return  addnode.attributes["value"].value;

            return "";

    public class webconfigconnectionstrings : webconfigbase

        internal  webconfigconnectionstrings(ref xmlnode xnode)

                if (addnode.attributes != null && addnode.attributes["name"].value == key)

                    addnode.attributes["connectionstring"].value = value;

                    return addnode.attributes["connectionstring"].value;

}

網站安裝打包 webconfig修改[三]

 下面看一下界面的操作方法:

網站安裝打包 webconfig修改[三]
網站安裝打包 webconfig修改[三]

 webconfighelper allconfig = new webconfighelper(allconfigpath);

            if (allconfig.loadisok)

                webconfigappsetting app = allconfig.appsetting;

                if (app != null)

                    app.set("mosftpusername", txtmosftpusername.text);

                    app.set("mosftppassword", txtmosftppassword.text);

                    app.set("providefor", cbbprovidefor.text);

                 }

                webconfigconnectionstrings connstring = allconfig.connectionstrings;

                if (connstring != null)

                    connstring.set("conn", txtconn.text);

                allconfig.save();

                allconfig.dispose();

                messagebox.show("配置檔案修改成功!");

網站安裝打包 webconfig修改[三]

 這裡提示一下,web.config中,不要帶名稱空間,就是xmlns="xxxx一大堆的";

打完,收工!