.net 2.0 中對設定檔的讀寫

來源:互聯網
上載者:User

在基於 .net 2.0 的企業庫中,原來的配置應用程式塊被廢除了,使用了 .net 2.0 內建的讀寫配置功能,下面我們就來看看 .net 2.0 中讀寫配置的功能。

即:  ConfigurationManager  類

注意:
ConfigurationManager 是處理用戶端應用程式設定檔的首選方法;不推薦使用任何其他方法。
對於 Web 應用程式,建議使用 WebConfigurationManager 類。

這個類的  AppSettings 屬性 在以前1.0 的時候,就有了, 2.0 中增加了 ConnectionStrings 屬性。
這些都不是今天我們要探討的內容,我們今天要探討的內容,是把一個配置類儲存到設定檔中,以及把這個配置類從設定檔中執行個體化出來。

這個配置類,必須是 派生自
System.Configuration.ConfigurationSection 類

如下面的類就是一個配置類

using System.Text;
using System.Configuration;
namespace ConfigTest
{
    class ConfigDataClass : ConfigurationSection
    {
        public ConfigDataClass()
        { }

        [ConfigurationProperty("id")]
        public int ID{
            get{return (int)this["id"];}
            set{   this["id"] = value;}
        }

        [ConfigurationProperty("name")]
        public string Name{
            get{ return this["name"].ToString();}
            set{ this["name"] = value;}
        }

        public override string ToString(){
            StringBuilder info = new StringBuilder();
            info.AppendFormat("id = {0};name = {1}", ID, Name);
            return info.ToString();
        }
    }
}

先說如何把這個配置類更新到設定檔中

// 配置資訊類初始化
ConfigDataClass configData = new ConfigDataClass();
configData.ID = 100;
configData.Name = "我是誰?";

// 開啟當前檔案的設定檔
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// 幹掉原先的配置
config.Sections.Remove("SettingsData");
// 把新的配置更新上去
config.Sections.Add("SettingsData", configData);
// 儲存設定檔
config.Save();

MessageBox.Show(configData.ToString());

讀取配置資訊

ConfigDataClass configData = ConfigurationManager.GetSection("SettingsData") as ConfigDataClass;
if (configData == null) return;
MessageBox.Show(configData.ToString());

當檔案修改的時候,自動從新登入設定檔需求

這個更簡單,只需要使用一個 System.IO.FileSystemWatcher 對象即可
private FileSystemWatcher watcher;

在初始化的時候,訂閱檔案改變事件。

// Initialize file system watcher
watcher = new FileSystemWatcher(AppDomain.CurrentDomain.BaseDirectory);
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
watcher.EnableRaisingEvents = false;

然後在 watcher_Changed 方法中,

private void watcher_Changed(object sender, FileSystemEventArgs e)
{
    if (e.FullPath.ToLower().Contains(".config"))
    {
  for (int i = 0; i < 3; i++)
  {
   try
   {
    // Using the static method, read the cached configuration settings
    ConfigurationManager.RefreshSection("EditorSettings");
    break;
   }
   catch (ConfigurationErrorsException)
   {
    if (i == 2) throw;
    else Thread.Sleep(100);
   }
  }
    }
}

顯然,上述的功能已經能滿足我們的需求了,所以企業庫才廢棄了之前的組態管理應用程式塊。

聯繫我們

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