成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

.net Framework配置文件詳細操作指導手冊

開發 后端
.NET Framework配置文件的操作方法比較繁雜。我們可以通過這篇文章介紹的內容初步的了解其中的操作技巧,并在實際應用中區鞏固所掌握知識。

.NET Framework的應用可以幫助開發人員創建一個支持WEB應用程序部署的平臺。方便編程人員進行代碼編寫。我們將會在文章中為大家詳細介紹有關.NET Framework配置文件的操作方法,希望對大家有所幫助。#t#

.NET Framework配置文件1. 創建配置節類

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

  1. class ConfigSectionData : 
    ConfigurationSection   
  2. {   
  3. [ConfigurationProperty("id")]   
  4. public int Id   
  5. {   
  6. get { return (int)this["id"];   
  7. }   
  8. set { this["id"] = value;   
  9. }   
  10. }   
  11. [ConfigurationProperty("time")]   
  12. public DateTime Time   
  13. {   
  14. get   
  15. {   
  16. return (DateTime)this["time"];   
  17. }   
  18. set { this["time"] = value;   
  19. }   
  20. }   

.NET Framework配置文件2. 創建配置文件操作對象

  1. Configuration config = 
    ConfigurationManager.
    OpenExeConfiguration
    (ConfigurationUserLevel.None);   
  2. ConfigSectionData data = 
    new ConfigSectionData(); 
  3. data.Id = 1000data.Time = 
    DateTime.Now; 
  4. config.Sections.Add("add", data);   
  5. config.Save(Configuration
    SaveMode.Minimal); 

上面的例子是操作 app.config,在根節點(configuration)下寫入名稱為"add"的配置數據。

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

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

  1. ExeConfigurationFileMap 
  2. file = new ExeConfigurationFileMap();   
  3. file.ExeConfigFilename = "test.config";   
  4. Configuration config = ConfigurationManager.
    OpenMappedExeConfiguration(file, 
    ConfigurationUserLevel.None);   
  5. ConfigSectionData data = 
    new ConfigSectionData();  
  6. data.Id = 1000data.Time = DateTime.Now;  
  7. config.Sections.Add("add", data);   
  8. config.Save(ConfigurationSaveMode.Minimal); 

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

  1. ExeConfigurationFileMap 
  2. file = new ExeConfigurationFileMap();   
  3. file.ExeConfigFilename = "test.config";   
  4. Configuration config = ConfigurationManager.
    OpenMappedExeConfiguration(file, 
    ConfigurationUserLevel.None);   
  5. ConfigSectionData data = 
    new ConfigSectionData(); data.Id = 1000;   
  6. data.Time = DateTime.Now;   
  7. config.SectionGroups.Add("group1", 
    new ConfigurationSectionGroup());   
  8. config.SectionGroups["group1"].
    Sections.Add("add", data);   
  9. config.Save(ConfigurationSaveMode.Minimal); 

下面就是生成的配置文件。

.NET Framework配置文件3. 讀取配置文件

 

  1. ExeConfigurationFileMap   
  2. file = new ExeConfigurationFileMap();   
  3. file.ExeConfigFilename = "test.config";   
  4. Configuration config =ConfigurationManager.
    OpenMappedExeConfiguration(file, 
    ConfigurationUserLevel.None);   
  5. ConfigSectionData data = config.
    SectionGroups["group1"].
    Sections["add"] as ConfigSectionData;   
  6. //ConfigSectionData data = 
    config.Sections["add"] as 
    ConfigSectionData;   
  7. // 從根節讀取   
  8. if (data != null)   
  9. {   
  10. Console.WriteLine(data.Id);   
  11. Console.WriteLine(data.Time);   
  12. }  

 

.NET Framework配置文件4. 寫配置文件

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

  1. ExeConfigurationFileMap file = 
    new ExeConfigurationFileMap();   
  2. file.ExeConfigFilename = "test.config";   
  3. Configuration config = ConfigurationManager.
    OpenMappedExeConfiguration(file, 
    ConfigurationUserLevel.None);   
  4. ConfigSectionData data = 
    new ConfigSectionData();   
  5. data.Id = 2000data.Time = DateTime.Now;   
  6. ConfigurationSectionGroup group1 = 
    config.SectionGroups["group1"];   
  7. if (group1 == null) config.SectionGroups.
    Add("group1", new ConfigurationSectionGroup());   
  8. ConfigurationSection data = 
    group1.Sections["add"] as config;   
  9. if (add == null) config.SectionGroups
    ["group1"].Sections.Add("add", data);  
  10. else { group1.Sections.Remove("add");   
  11. group1.Sections.Add("add", data);   
  12. // 或者直接修改原配置對象,前提是類型轉換要成功。   
  13. //ConfigSectionData configData = 
    add as ConfigSectionData;   
  14. //configData.Id = data.Id;   
  15. //configData.Time = data.Time;   
  16. }   
  17. config.Save(ConfigurationSaveMode.Minimal); 

#p#

.NET Framework配置文件5. 刪除配置節

刪除ConfigurationSectionGroup

  1. config.SectionGroups.
    Remove("group1");   
  2. //config.SectionGroups.Clear();   
  3. config.Save(Configuration
    SaveMode.Minimal); 

刪除ConfigurationSection

  1. config.Sections.Remove("add1");   
  2. //config.Sections.Clear();   
  3. if (config.SectionGroups["group1"] 
    != null)   
  4. {   
  5. config.SectionGroups["group1"].
    Sections.Remove("add2");   
  6. //config.SectionGroups["group1"].
    Sections.Clear();   
  7. }   
  8. config.Save(Configuration
    SaveMode.Minimal); 

.NET Framework配置文件6. 其他

可以使用 ConfigurationManager.OpenMachineConfiguration() 來操作 Machine.config 文件。 或者使用 System.Web.Configuration 名字空間中的 WebConfigurationManager 類來操作 ASP.net 配置文件。 ConfigurationManager還提供了AppSettings、ConnectionStrings、GetSection()等便捷操作。

.NET Framework配置文件7. 使用自定義類

比如ConfigSectionData里面,除了簡單類型之外,可不可以有自定義的類。可以使用自定義類,不過需要定義一個轉換器。

  1. using System;   
  2. using System.Collections; using System.
    Collections.Generic;   
  3. using System.Configuration; using 
    System.Globalization;   
  4. using System.ComponentModel;   
  5. // 要寫入配置文件的自定義類   
  6. class CustomData   
  7. {   
  8. public CustomData(string s)   
  9. {   
  10. this.s = s;   
  11. }   
  12. private string s;   
  13. public string S   
  14. {   
  15. get   
  16. {   
  17. return s;   
  18. }   
  19. set   
  20. {   
  21. s = value;   
  22. }   
  23. }   
  24. }   
  25. // 自定義的轉換器(演示代碼省略了類型判斷)   
  26. class CustomConvert : Configuration
    ConverterBase   
  27. {  
  28. public override bool CanConvertFrom
    (ITypeDescriptorContext ctx, Type type)   
  29. {   
  30. return (type == typeof(string));   
  31. }   
  32. public override object ConvertTo
    (ITypeDescriptorContext ctx, 
    CultureInfo ci, object value, Type type)   
  33. {   
  34. return (value as CustomData).S; 
  35. public override object ConvertFrom
    (ITypeDescriptorContext ctx, 
    CultureInfo ci, object data)   
  36. {   
  37. return new CustomData((string)data);;   
  38. }   
  39. }  
  40. class ConfigSectionData : 
    ConfigurationSection   
  41. { [ConfigurationProperty("id")]   
  42. public int Id   
  43. {   
  44. get   
  45. {   
  46. return (int)this["id"];   
  47. }   
  48. set   
  49. {   
  50. this["id"] = value;   
  51. }   
  52. }   
  53. [ConfigurationProperty("time")]   
  54. public DateTime Time { get   
  55. {   
  56. return (DateTime)this["time"];   
  57. }   
  58. set   
  59. {   
  60. this["time"] = value;   
  61. }   
  62. }   
  63. [ConfigurationProperty("custom")] 
    [TypeConverter(typeof(CustomConvert))]   
  64. // 指定轉換器 public CustomData Custom   
  65. {   
  66. get   
  67. {   
  68. return (CustomData)this["custom"];   
  69. } set   
  70. {   
  71. this["custom"] = value;   
  72. }   
  73. }   
  74. }   
  75. public class Program   
  76. {   
  77. static void Main(string[] args)   
  78. {   
  79. Configuration config = ConfigurationManager.
    OpenExeConfiguration(ConfigurationUserLevel.None);   
  80. ConfigSectionData data = new ConfigSectionData();   
  81. data.Id = 1000;   
  82. data.Time = DateTime.Now;  
  83.  data.Custom = new CustomData("abcdefg...");   
  84. config.Sections.Add("add", data); 
  85. config.Save(ConfigurationSaveMode.Minimal);   
  86. // 讀取測試 
  87. ConfigSectionData configData = 
    (ConfigSectionData)config.Sections["add"];   
  88. bbs.bitsCN.com Console.WriteLine
    (configData.Custom.S);   
  89. }   
  90. }  

.NET Framework配置文件的相關操作方法就為大家介紹到這里。

責任編輯:曹凱 來源: IT168
相關推薦

2009-12-22 17:14:37

ADO.NET Ent

2010-01-06 15:29:13

.NET Framew

2010-06-03 14:55:59

Hadoop配置

2010-01-04 16:17:50

Silverlight

2010-01-06 11:30:22

.NET Framew

2010-01-06 19:09:13

.NET Framew

2009-12-15 11:28:34

.NET Framew

2010-09-26 10:02:09

JVM優化配置

2011-04-19 14:35:58

ASP.NETWeb.config

2010-02-22 10:18:18

WCF配置文件

2009-12-22 14:46:09

ADO.NET Ent

2009-12-22 11:14:38

WCF禁用安全配置

2009-12-16 16:51:05

Ruby on Rai

2010-01-06 18:13:58

.NET Framew

2010-01-06 19:18:22

.NET Framew

2010-01-05 17:39:10

.NET Framew

2010-01-06 13:50:37

.NET Framew

2010-06-07 18:12:39

mrtg 安裝

2010-02-23 14:17:20

WCF配置文件

2009-07-21 10:05:10

ASP.NET配置文件
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 一区二区三区亚洲 | 久久久久久久久毛片 | 97视频在线免费 | 国产精彩视频在线观看 | 99视频免费播放 | 成人在线观看免费视频 | 一级毛片色一级 | 在线免费毛片 | 午夜精品福利视频 | 欧美成人一区二免费视频软件 | 亚洲免费人成在线视频观看 | 天天狠狠 | 欧美精品在线播放 | 日韩欧美在线免费观看 | 亚洲精品一区二区三区中文字幕 | 精品国产不卡一区二区三区 | 污片在线观看 | 精品久久久久久亚洲精品 | 亚洲一区二区三区久久 | 青青草国产在线观看 | 91在线电影 | 天天躁日日躁xxxxaaaa | 91在线观看| 国产成人精品亚洲日本在线观看 | 在线观看中文字幕视频 | 日韩电影一区 | 91精品国产91久久久久久 | 亚洲精品www久久久久久广东 | 国产精品久久久久久一区二区三区 | 日韩精品区 | 伊人精品在线视频 | 在线免费观看欧美 | 99精品国产一区二区青青牛奶 | 一区二区免费 | 二区视频 | 阿v视频在线观看 | 成人午夜影院 | 国产日韩欧美一区 | 亚洲欧美视频 | 日韩精品在线网站 | 国产 91 视频 |