You're here: Snippet Directory » Microsoft .NET (154)
Language:

Konfigurationseinstellungen auslesen

Language: Deutsch
Programming Language: C#
Published by: Roladn [not registered]
Last Update: 5/4/2006
Views: 730

Description

Liest Konfigurationseinstellungen mit .NET (1.x) aus.

Code

1 using System; 2 using System.Data; 3 using System.IO; 4 5 namespace justControl 6 { 7 /// <summary> 8 /// Original was written in VB.NET and is published on 9 /// http://www.codeproject.com/vb/net/ConfigOpt.asp 10 /// </summary> 11 public class ConfigOpt 12 { 13 // Inside this DataSet a single DataTable named ConfigValues is created 14 private static DataSet DSoptions; 15 // this is the filename for the DataSet XML serialization 16 private static string mConfigFileName; 17 18 // This property is read-only, because it is set 19 // through Initialize or Store methods 20 public static string ConfigFileName 21 { 22 get 23 { 24 return mConfigFileName; 25 } 26 } 27 28 // This method has to be invoked before using 29 // any other method of ConfigOpt class 30 // ConfigFile parameter is the name of the config file to be read 31 // (if that file doesn't exists, the method 32 // simply initialize the data structure 33 // and the ConfigFileName property) 34 public static void Initialize(string ConfigFile) 35 { 36 mConfigFileName = ConfigFile; 37 DSoptions = new DataSet("ConfigOpt"); 38 if(File.Exists(ConfigFile)) 39 { 40 // populate DataSet 41 DSoptions.ReadXml(ConfigFile); 42 } 43 else 44 { 45 // Initialize DataSet with two fields (key / value) 46 // DataSet is left empty 47 DataTable dt = new DataTable("ConfigValues"); 48 dt.Columns.Add("OptionName", System.Type.GetType("System.String")); 49 dt.Columns.Add("OptionValue", System.Type.GetType("System.String")); 50 DSoptions.Tables.Add(dt); 51 } 52 } 53 54 // This method serializes the memory data 55 // structure holding the config parameters 56 // The filename used is the one defined calling Initialize method 57 public static void Store() 58 { 59 Store(mConfigFileName); 60 } 61 62 // Same as Store() method, but with the ability 63 // to serialize on a different filename 64 public static void Store(string ConfigFile) 65 { 66 mConfigFileName = ConfigFile; 67 DSoptions.WriteXml(ConfigFile); 68 } 69 70 // Read a configuration Value (aka OptionValue), 71 // given its Key (aka OptionName) 72 // If the Key is not defined, an empty string is returned 73 public static string GetOption(string OptionName) 74 { 75 DataView dv = DSoptions.Tables["ConfigValues"].DefaultView; 76 dv.RowFilter = "OptionName='" + OptionName + "'"; 77 if(dv.Count > 0) 78 return dv[0]["OptionValue"].ToString(); 79 else 80 return ""; 81 } 82 83 // Write in the memory data structure a Key/Value 84 // pair for a configuration setting 85 // If the Key already exists, the Value is simply updated, 86 // else the Key/Value pair is added 87 // Warning: to update the written Key/Value pair 88 // on the config file, you need to call Store 89 public static void SetOption(string OptionName, string OptionValue) 90 { 91 DataView dv = DSoptions.Tables["ConfigValues"].DefaultView; 92 dv.RowFilter = "OptionName='" + OptionName + "'"; 93 if(dv.Count > 0) 94 { 95 dv[0]["OptionValue"] = OptionValue; 96 } 97 else 98 { 99 DataRow dr = DSoptions.Tables["ConfigValues"].NewRow(); 100 dr["OptionName"] = OptionName; 101 dr["OptionValue"] = OptionValue; 102 DSoptions.Tables["ConfigValues"].Rows.Add(dr); 103 } 104 } 105 106 public ConfigOpt() 107 { 108 } 109 } 110 } 111

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS