熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

.Net Framework提供的配置文件操作

2013-11-13 10:29:33  來源: .NET編程 

  創建配置節類

  必須創建繼承自ConfigurationSection的對象才能進行配置數據讀寫操作ConfigurationSection提供了索引器用來獲取和設置配置數據需要注意的是擁有ConfigurationProperty特性的屬性才會被存儲並且名稱要保持大小寫完全一致如下面的代碼中所有的id必須保持一樣

  class ConfigSectionData : ConfigurationSection

  {

  [ConfigurationProperty(id)]

  public int Id

  {

  get { return (int)this[id]; }

  set { this[id] = value; }

  }

  [ConfigurationProperty(time)]

  public DateTime Time

  {

  get { return (DateTime)this[time]; }

  set { this[time] = value; }

  }

  }

   創建配置文件操作對象

  Configuration config = ConfigurationManagerOpenExeConfiguration(ConfigurationUserLevelNone)

  ConfigSectionData data = new ConfigSectionData()

  dataId = ;

  dataTime = DateTimeNow;

  configSectionsAdd(add data)

  configSave(ConfigurationSaveModeMinimal)

  上面的例子是操作nfig在根節點(configuration)下寫入名稱為add的配置數據

  需要注意的 VS 在IDE模式下會將信息寫入*nfig並且在程序關閉時覆寫該文件因此您可能看不到您寫入的配置數據只要在資源管理其中執行*exe 文件您就可以在*nfig 文件中看到結果了

  如果我們需要操作非缺省配置文件可以使用ExeConfigurationFileMap對象

  ExeConfigurationFileMap file = new ExeConfigurationFileMap()

  fileExeConfigFilename = nfig;

  Configuration config = ConfigurationManagerOpenMappedExeConfiguration(file ConfigurationUserLevelNone)

  ConfigSectionData data = new ConfigSectionData()

  dataId = ;

  dataTime = DateTimeNow;

  configSectionsAdd(add data)

  configSave(ConfigurationSaveModeMinimal)

  如果我們不希望在根節點下寫入配置數據可以使用ConfigurationSectionGroup對象

  ExeConfigurationFileMap file = new ExeConfigurationFileMap()

  fileExeConfigFilename = nfig;

  Configuration config = ConfigurationManagerOpenMappedExeConfiguration(file

  ConfigurationUserLevelNone)

  ConfigSectionData data = new ConfigSectionData()

  dataId = ;

  dataTime = DateTimeNow;

  configSectionGroupsAdd(group new ConfigurationSectionGroup())

  configSectionGroups[group]SectionsAdd(add data)

  configSave(ConfigurationSaveModeMinimal)

  下面就是生成的配置文件

   讀取配置文件

  ExeConfigurationFileMap file = new ExeConfigurationFileMap()

  fileExeConfigFilename = nfig;

  Configuration config = ConfigurationManagerOpenMappedExeConfiguration(file ConfigurationUserLevelNone)

  ConfigSectionData data = configSectionGroups[group]Sections[add] as ConfigSectionData;

  //ConfigSectionData data = configSections[add] as ConfigSectionData; // 從根節讀取

  if (data != null)

  {

  ConsoleWriteLine(dataId)

  ConsoleWriteLine(dataTime)

  }

   寫配置文件

  在寫入 ConfigurationSectionGroup 和 ConfigurationSection 前要判斷同名配置是否已經存在否則會寫入失敗 另外如果配置文件被其他Configuration對象修改則保存會失敗並拋出異常建議采用Singleton模式 ExeConfigurationFileMap file = new ExeConfigurationFileMap()

  fileExeConfigFilename = nfig;

  Configuration config = ConfigurationManagerOpenMappedExeConfiguration(file ConfigurationUserLevelNone)

  ConfigSectionData data = new ConfigSectionData()

  dataId = ;

  dataTime = DateTimeNow;

  ConfigurationSectionGroup group = configSectionGroups[group];

  if (group == null)

  configSectionGroupsAdd(group new ConfigurationSectionGroup())

  ConfigurationSection data = groupSections[add] as config;

  if (add == null)

  configSectionGroups[group]SectionsAdd(add data)

  else

  {

  groupSectionsRemove(add

  groupSectionsAdd(add data)

  // 或者直接修改原配置對象前提是類型轉換要成功

  //ConfigSectionData configData = add as ConfigSectionData;

  //configDataId = dataId;

  //configDataTime = dataTime;

  }

  configSave(ConfigurationSaveModeMinimal)

   刪除配置節

  刪除ConfigurationSectionGroup  configSectionGroupsRemove(group

  //configSectionGroupsClear()

  configSave(ConfigurationSaveModeMinimal)

  刪除ConfigurationSection  configSectionsRemove(add

  //configSectionsClear()

  if (configSectionGroups[group] != null)

  {

  configSectionGroups[group]SectionsRemove(add

  //configSectionGroups[group]SectionsClear()

  }

  configSave(ConfigurationSaveModeMinimal) 其他

  可以使用 ConfigurationManagerOpenMachineConfiguration() 來操作 nfig 文件 或者使用 SystemWebConfiguration 名字空間中的 WebConfigurationManager 類來操作 配置文件

  ConfigurationManager還提供了AppSettingsConnectionStringsGetSection()等便捷操作 使用自定義類

  比如ConfigSectionData裡面除了簡單類型之外可不可以有自定義的類? 可以使用自定義類不過需要定義一個轉換器 using System;

  using SystemCollections;

  using SystemCollectionsGeneric;

  using SystemConfiguration;

  using SystemGlobalization;

  using SystemComponentModel;

  // 要寫入配置文件的自定義類

  class CustomData

  {

  public CustomData(string s)

  {

  thiss = s;

  }

  private string s;

  public string S

  {

  get { return s; }

  set { s = value; }

  }

  }

  // 自定義的轉換器(演示代碼省略了類型判斷)

  class CustomConvert : ConfigurationConverterBase

  {

  public override bool CanConvertFrom(ITypeDescriptorContext ctx Type type)

  {

  return (type == typeof(string))

  }

  public override object ConvertTo(ITypeDescriptorContext ctx CultureInfo ci object value Type type)

  {

  return (value as CustomData)S;

  }

  public override object ConvertFrom(ITypeDescriptorContext ctx CultureInfo ci object data)

  {

  return new CustomData((string)data)

  }

  }

  class ConfigSectionData : ConfigurationSection

  {

  [ConfigurationProperty(id)]

  public int Id

  {

  get { return (int)this[id]; }

  set { this[id] = value; }

  }

  [ConfigurationProperty(time)]

  public DateTime Time

  {

  get { return (DateTime)this[time]; }

  set { this[time] = value; }

  }

  [ConfigurationProperty(custom)]

  [TypeConverter(typeof(CustomConvert))] // 指定轉換器

  public CustomData Custom

  {

  get { return (CustomData)this[custom]; }

  set { this[custom] = value; }

  }

  }

  public class Program

  {

  static void Main(string[] args)

  {

  Configuration config = ConfigurationManagerOpenExeConfiguration (ConfigurationUserLevelNone)

  ConfigSectionData data = new ConfigSectionData()

  dataId = ;

  dataTime = DateTimeNow;

  dataCustom = new CustomData(abcdefg…

  configSectionsAdd(add data)

  configSave(ConfigurationSaveModeMinimal)

  // 讀取測試

  ConfigSectionData configData = (ConfigSectionData)configSections[add];

  ConsoleWriteLine(configDataCustomS)

  }

  } 保存後的配置文件


From:http://tw.wingwit.com/Article/program/net/201311/13806.html
  • 上一篇文章:

  • 下一篇文章:
  • Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.