C# 如何擷取自訂的config中節點的值,並修改節點的值

來源:互聯網
上載者:User

標籤:connect   set   ace   help   pdo   dex   using   exe   不為   

現定義一個方法 DIYConfigHelper.cs

using System;using System.Xml;using System.Configuration;using System.Reflection;using System.Web;using System.IO;namespace Chain.Common{    /// <summary>    /// Summary description for ReadWriteConfig.    /// </summary>    public class DIYConfigHelper    {        /// <summary>        ///         /// </summary>        /// <param name="key">節點名稱</param>        /// <returns></returns>        /// <summary>        /// 擷取自訂 index.config 檔案中的 appsetting 節點值        /// </summary>        /// <param name="path">config檔案的路徑</param>        /// <param name="key">節點名稱</param>        /// <returns>節點名稱的值</returns>        public static string GetIndexConfigValue(string path, string key)        {            string indexConfigPath = path;            if (string.IsNullOrEmpty(indexConfigPath))                throw new Exception("請檢查應用程式設定檔 appSettings 節點,是否存在 indexConfig 且 value 不為空白的配置節!");            if (!File.Exists(indexConfigPath))                throw new Exception(string.Format("設定檔不存在:{0}", indexConfigPath));            ExeConfigurationFileMap ecf = new ExeConfigurationFileMap();            ecf.ExeConfigFilename = indexConfigPath;            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(ecf, ConfigurationUserLevel.None);            return config.AppSettings.Settings[key].Value;        }        /// <summary>        /// 設定自訂 index.config 檔案中的 appsetting 節點值        /// </summary>        /// <param name="path">config檔案的路徑</param>        /// <param name="key">節點名稱</param>        /// <param name="value">需要修改的值</param>        /// <returns>true:修改成功 false:修改失敗</returns>        public static bool SetIndexConfigValue(string path, string key, string value)        {            string indexConfigPath = path;            if (string.IsNullOrEmpty(indexConfigPath))                throw new Exception("請檢查應用程式設定檔 appSettings 節點,是否存在 indexConfig 且 value 不為空白的配置節!");            if (!File.Exists(indexConfigPath))                throw new Exception(string.Format("設定檔不存在:{0}", indexConfigPath));            ExeConfigurationFileMap ecf = new ExeConfigurationFileMap();            ecf.ExeConfigFilename = indexConfigPath;            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(ecf, ConfigurationUserLevel.None);            config.AppSettings.Settings[key].Value = value;            config.Save();            return true;        }    }}

調用方式:

string ss = Chain.Common.DIYConfigHelper.GetIndexConfigValue(HttpRuntime.AppDomainAppPath.ToString() + "DIY.config", "14");bool tt = Chain.Common.DIYConfigHelper.SetIndexConfigValue(HttpRuntime.AppDomainAppPath.ToString() + "DIY.config", "14", "5");

DIY.config檔案的內容

<?xml version="1.0" encoding="utf-8"?><configuration>  <appSettings>     <add key="15" value="663CFB4AF7AE2A91B14587C31B3DE60AF38AED2E63F5040C5D453CBC704162B8ACDD7A7D67A95FA0" />     <add key="14" value="156D7DB054ABBF9B321B1E8982130FDA3420475BC524C4259C55A8CEA4F884DE649FD16284A1053F" />  </appSettings>  <connectionStrings /></configuration>

 

C# 如何擷取自訂的config中節點的值,並修改節點的值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.