Around and About .NET World

Il blog di Marco Minerva
posts - 1671, comments - 2232, trackbacks - 2135

My Links

News

Contattami su Live Messenger:


MCTS: Windows, Web, Distributed Applications & SQL Server

MCPD: Enterprise Applications

Tag Cloud

Archives

Post Categories

Links

App.config con il .NET Compact Framework

Il .NET Compact Framework non offre un supporto nativo per la lettura/scrittura del file app.config, rendendo necessario l'utlizzo esplicito dell'oggetto XmlDocument per lavorare con esso. La seguente classe si prende carico di svolgere in automatico le operazioni necessarie:

using System; using System.Xml; using System.Windows.Forms; using System.Configuration; using System.IO; using System.Reflection; namespace System.Configuration { public class ConfigurationSettings { private static string configFileName; private const string DEFAULT_SECTION = "appSettings"; static ConfigurationSettings() { string AppPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase); string EXEName = Assembly.GetExecutingAssembly().GetName().Name; configFileName = Path.Combine(AppPath, EXEName) + ".exe.config"; } public static string Get(string Key) { return Get(Key, DEFAULT_SECTION); } public static string Get(string Key, string Section) { string str = string.Empty; XmlDocument doc = new XmlDocument(); doc.Load(configFileName); string nodeKey = "//" + Section + "/add[@key='" + Key + "']/@value"; if (doc.DocumentElement.SelectSingleNode(nodeKey) != null) str = doc.DocumentElement.SelectSingleNode(nodeKey).Value; return str; } public static void Set(string Key, string Value) { Set(Key, DEFAULT_SECTION, Value); } public static void Set(string Key, string Section, string Value) { XmlDocument doc = new XmlDocument(); doc.Load(configFileName); string nodeKey = "//" + Section + "/add[@key='" + Key + "']/@value"; if (doc.DocumentElement.SelectSingleNode(nodeKey) != null) { doc.DocumentElement.SelectSingleNode(nodeKey).Value = Value; } else { XmlElement add = doc.CreateElement("add"); XmlAttribute attrkey = doc.CreateAttribute("key"); attrkey.Value = Key; XmlAttribute val = doc.CreateAttribute("value"); val.Value = Value; add.Attributes.Append(attrkey); add.Attributes.Append(val); doc.DocumentElement.SelectSingleNode("//" + Section).AppendChild(add); } doc.Save(configFileName); } } }

Questo codice consente di gestire file di configurazione con il seguente formato:

<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="SettingName" value="SettingValue" /> </appSettings> </configuration>

L'utilizzo รจ immediato:

//Legge un valore. string value = ConfigurationSettings.Get("SettingName"); //Scrive un valore. ConfigurationSettings.Set("SettingName", "NewValue");

Print | posted on Tuesday, January 30, 2007 11:26 AM | Filed Under [ .NET Compact Framework ]

Feedback

Gravatar

# re: App.config con il .NET Compact Framework

Molto utile grazie.
1/30/2007 12:48 PM | http://www.pcinformatica.eu
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET