xml 方式更新和擷取 設定檔 appSettings 節點 解決辦法

來源:互聯網
上載者:User

標籤:

最近在搞一個小程式,會用到動態修改設定檔來進行處理,在百度上找了很多辦法,但是始終達不到我預想的效果,先列出程式運行環境和開發工具版本:

開發工具:VS2010

.Net 運行環境:4.0

有兩種方式,分別如下:

第一種方式:只能在程式運行和調試時有效,在程式打包成安裝包並安裝之後會出現問題,完整代碼如下:

/// <summary>        /// 設定設定檔key對應的值        /// </summary>        /// <param name="key">鍵</param>        /// <param name="value">值</param>        /// <returns></returns>        public static bool SetAppSetByKey(string key, string value)        {            bool result = false;            try            {                Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);                cfa.AppSettings.Settings[key].Value = value;                cfa.Save();                ConfigurationManager.RefreshSection("appSettings");        //必須重新整理                result = true;            }            catch (Exception)            {                result = false;            }            return result;        }        /// <summary>        /// 新增設定檔key對應的值        /// </summary>        /// <param name="key">鍵</param>        /// <param name="value">值</param>        /// <returns></returns>        public static bool AddAppSetByKey(string key, string value)        {            bool result = false;            try            {                Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);                cfa.AppSettings.Settings.Add(key, value);                cfa.Save();                ConfigurationManager.RefreshSection("appSettings");        //必須重新整理                result = true;            }            catch (Exception)            {                result = false;            }            return result;        }

第二種方式:能解決以上問題,但是當程式安裝在C盤時會出現設定檔無法訪問的情況,完整代碼如下:

///<summary>        ///在config檔案中appSettings配置節增加一對鍵、值對,如果已經存在先移除再添加.        ///</summary>        ///<param name="newKey">新鍵</param>        ///<param name="newValue">新值</param>        public static void SetAppSetByKey(string newKey, string newValue)        {            XmlDocument doc = new XmlDocument();            doc.Load(AppConfig());            XmlNode node = doc.SelectSingleNode(@"//appSettings");            XmlElement ele = (XmlElement)node.SelectSingleNode(@"//add[@key=‘" + newKey + "‘]");            ele.SetAttribute("value", newValue);            doc.Save(AppConfig());        }        /// <summary>        /// 擷取配置節點        /// </summary>        /// <param name="configName">要擷取的鍵</param>        /// <returns></returns>        public static string GetAppSetByKey(string configName)        {            XmlDocument xDoc = new XmlDocument();            try            {                xDoc.Load(AppConfig());                XmlNode xNode;                XmlElement xElem;                xNode = xDoc.SelectSingleNode("//appSettings");    //補充,需要在你的app.config 檔案中增加一下,<appSetting> </appSetting>                xElem = (XmlElement)xNode.SelectSingleNode("//add[@key=‘" + configName + "‘]");                if (xElem != null)                    return xElem.GetAttribute("value");                else                    return "";            }            catch (Exception)            {                return "";            }        }        /// <summary>        /// 設定檔根目錄        /// </summary>        /// <returns></returns>        public static string AppConfig()        {            return System.IO.Path.Combine(Application.StartupPath.Trim(), Application.ProductName + ".exe.config");        }

順便提提,方式二,參考文獻來源於:http://www.cnblogs.com/Fooo/archive/2012/12/03/2799714.html

此時已完成一半,接下來處理無法存取權限的問題:

將程式設定成已管理員身份運行

 

如何讓程式在啟動時,自動要求“管理員”許可權了,我們只需要修改app.manifest檔案中的配置項即可。
  app.manifest檔案預設是不存在的,我們可以通過以下操作來自動添加該檔案。
        (1)進入項目屬性頁面。
        (2)選擇“安全性”欄目。
        (3)將“啟用ClickOnce安全設定”勾選上。
  現在,在Properties目錄下就自動產生了app.manifest檔案,開啟該檔案,將 trustInfo/security/requestedPrivileges節點的requestedExecutionLevel的level 的值修改為requireAdministrator即可。如下所示:
    <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
         <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> ;
    </requestedPrivileges>

 

   (4)記住,如果不需要ClickOnce,可以回到項目屬性頁面將“啟用ClickOnce安全設定”不勾選。
   (5)接下來,重新編譯你的程式就OK了。

 

參考文獻來源於:http://www.2cto.com/Article/201112/115471.html

設定後在編譯時間會出現一個異常:ClickOnce 不支援要求執行層級"requireAdministrator"......       ——將設定成管理員身份運行步驟裡的溝去掉即可

處理以上異常的解決方案參考百度:http://zhidao.baidu.com/link?url=v9xx3nSK8HOES1d0YXoTLRkEACaMmDllyNMz_CNBIP2RSKsFNvHsT7SI5UDrQaqp5c6aJRLAB80HOuoJky0A_a

 

至此,問題已經解決!!!若有疑問請留言交流,請各位大神多多指點

xml 方式更新和擷取 設定檔 appSettings 節點 解決辦法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.