Language:

XML-Config (App.Config) mit dem Compact Framework nutzen

Language: Deutsch
Programming Language: C#
Published by: Thomas
Last Update: 5/22/2006
Views: 4963

Description

Die Klasse ermöglicht es auf App-Settings gespeichert in einer XML-Datei (hier Config.xml) zuzugreifen und diese auch zu ändern. Damit hat man den idealen und schnelle Datenspeicher für kleine Anwendungs-Einstellungen.

Die Klasse stammt im Original von Golo Haas und wurde von mir nur leicht für das Speichern/Ändern der Einstellungen geändert.

Code

1 using System; 2 using System.Collections.Specialized; 3 using System.IO; 4 using System.Reflection; 5 using System.Xml; 6 7 namespace LPPC.BLL.Utilities 8 { 9 /// 10 /// Represents a configuration settings manager for the Compact Framework. 11 /// 12 public static class ConfigurationSettings 13 { 14 /// 15 /// Contains the application settings collection. 16 /// 17 private static NameValueCollection _appSettings; 18 19 /// 20 /// Gets the application settings. 21 /// 22 /// The application settings. 23 public static NameValueCollection AppSettings 24 { 25 get 26 { 27 // If the application settings are not available yet, 28 // load them. 29 if (_appSettings == null) 30 { 31 Load(); 32 } 33 34 // Return the application settings to the caller. 35 return _appSettings; 36 } 37 } 38 39 /// 40 /// Loads the application settings. 41 /// 42 public static void Load() 43 { 44 // Get the application folder. 45 string applicationFolder = Path.GetDirectoryName( 46 Assembly.GetExecutingAssembly().GetName().CodeBase); 47 48 // Get the complete path to the App.config file. 49 string configFile = 50 Path.Combine(applicationFolder, "Config.xml"); 51 52 // If the App.config file does not exist, throw an exception. 53 if (!File.Exists(configFile)) 54 { 55 throw new ApplicationException("Config.xml does not exist!"); 56 } 57 58 // Load the App.config file. 59 XmlDocument xmlDocument = new XmlDocument(); 60 xmlDocument.Load(configFile); 61 62 // Select all application settings nodes. 63 XmlNodeList xmlNodeList = 64 xmlDocument.SelectNodes("/configuration/appSettings/add"); 65 66 // Initialize the application settings collection. 67 _appSettings = new NameValueCollection(); 68 69 // Iterate over all application settings nodes. 70 foreach (XmlNode xmlNode in xmlNodeList) 71 { 72 // Add the key and the value to the application settings. 73 AppSettings.Add(xmlNode.Attributes["key"].Value, 74 xmlNode.Attributes["value"].Value); 75 } 76 } 77 78 public static void Save(string key, string value) 79 { 80 // Get the application folder. 81 string applicationFolder = Path.GetDirectoryName( 82 Assembly.GetExecutingAssembly().GetName().CodeBase); 83 84 // Get the complete path to the App.config file. 85 string configFile = 86 Path.Combine(applicationFolder, "Config.xml"); 87 88 // If the App.config file does not exist, throw an exception. 89 if (!File.Exists(configFile)) 90 { 91 throw new ApplicationException("Config.xml does not exist!"); 92 } 93 94 // Load the App.config file. 95 XmlDocument xmlDocument = new XmlDocument(); 96 xmlDocument.Load(configFile); 97 98 // Iterate over all application settings nodes. 99 foreach (XmlNode xmlNode in xmlDocument.SelectNodes("/configuration/appSettings/add")) 100 { 101 102 if (xmlNode.Attributes["key"].Value == key) 103 { 104 xmlNode.Attributes["value"].Value = value; 105 break; 106 } 107 } 108 109 xmlDocument.Save(configFile); 110 111 } 112 113 } 114 } 115 // Beispiel-Config-File: 116 117 //<?xml version="1.0" encoding="utf-8" ?> 118 //<configuration> 119 // <appSettings> 120 // <add key="Language" value="2" /> 121 // <add key="Catalog" value="bla" /> 122 // </appSettings> 123 //</configuration>

2 comments

1

it's good work

Thursday, January 11, 2007 11:34:44 AM from Rave
2

Hallo,

hab's erst jetzt durch Zufall entdeckt, dass mein Snippet von damals hier weiterverwendet wurde, und fühle mich natürlich geehrt :-).

In diesem Sinne - danke schön!

Viele Grüße,


Golo

Monday, October 06, 2008 12:06:17 PM from Golo Roden

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS