- How can be handled configuration values of an application, allowing the user to possibly change the default values here defined?
Suppose you have defined in the App.config file some parameters assigning a predefined default value for each:<configuration> <appSettings> <add key="LoginType" value="DAILY" /> <add key="MergeAudio" value="TRUE" /> .... </appSettings> </configuration>Then you can define, for example in a Utility class, the following method: it tries to read the key from the IsolatedStorage;
if it failes, it loads, from the configuration file, the possible default value defined by the application and then it save it in the IsolatedStorage for a quicker further use. In fact, next time that function will be called, it will find that key in the IsolatedStorage and the data will be available in less time!public static string GetAppSettings(string key) { //return (GetAppConfigString("App.config", key)); //return null; String value = String.Empty; if (IsolatedStorageSettings.ApplicationSettings.Contains(key)) { value = IsolatedStorageSettings.ApplicationSettings[key].ToString(); } else { // se non c'è nell'isolated storage: cerca di caricare, con GetAppConfigString, il valore di default (se esiste) da app.config, per poi salvarlo nell'Isolated Storage value = GetAppConfigString("app.config", key); if (!value.Equals(String.Empty)) { SetAppSettings(key, GetAppConfigString("app.config", key)); } } return value; }Finally here it is the utility function to recover the default value of a key from the configuration file:
private static XElement _xmlConfig; private static Object _xmlConfigSyncLock = new Object(); public static string GetAppConfigString(string configName, string key) { if (_xmlConfig == null) { lock (_xmlConfigSyncLock) { _xmlConfig = XElement.Load(configName); } } XElement element = _xmlConfig.Descendants("appSettings").Descendants("add").FirstOrDefault((el) => el.FirstAttribute.Value == key); if (element != null) { XAttribute xAttribute = element.Attribute("value"); if (xAttribute != null) return xAttribute.Value; } return String.Empty; }When you need to get the key value from your code, you can simply call
#region App.config keys public const String LoginType = "LoginType"; ... #endregion App.config keys string loginTypeString = Utility.GetAppSettings(Constants.LoginType);and then when the user change that default value, you can update its value in the IsolatedStorage:
Utility.SetAppSettings(Constants.LoginType, LoginType.DAILY.ToString());
where, always in the Utility class:
public static string SetAppSettings(string key, string value) { IsolatedStorageSettings.ApplicationSettings[key] = value; IsolatedStorageSettings.ApplicationSettings.Save(); return value; }Note that data written to the IsolatedStorageSettings object hasn’t actually gotten committed to the isolated storage area and it is saved only when the application that uses the class is closed. This can occur when the user closes the Web browser, refreshes a page, or browses away from the page. If you want your application to write to isolated storage immediately, you have to call the Save method in application code.
-
Unisciti a 166 altri iscritti
Categorie
Supporta questo Blog
Translate
-
Articoli recenti
- Evento pubblico e gratuito “Quando si pranzava alle cinque del pomeriggio” di Alessandro Barbero al Museo del Risorgimento: come prenotare??? 30 novembre 2026
- Referendum e iniziative popolari: è possibile sottoscrivere la proposta di legge per la rivalutazione delle pensioni 22 novembre 2026
- Domenica 15/11/2026, ore 16:30 – Spettacolo teatrale di beneficenza al Centro Comunale Polivalente di Bricherasio: il Laboratorio dell’Arco replica la commedia brillante “La zia di Carlo” 14 novembre 2026
- I quartieri di periferia sempre più abbandonati da investimenti privati e pubblici: chiude tra una settimana la palestra OmniaFit di via Porpora a Torino in Barriera di Milano 7 novembre 2026
- La ricarica automatica su base soglia e il roaming marittimo su traghetto: un astuto stratagemma delle compagnie telefoniche per applicare al cliente dei costi non voluti né previsti 10 luglio 2026
Archivi
Classifica Post
- Frattura biossea (tibia e perone) scomposta della gamba: istruzioni per l'uso
- Come sapere se un rubinetto (e.g. per lavatrice o del gas) è aperto o chiuso?
- Review e test dello scanner per diapositive REFLECTA - DigitDia 6000
- Review & test di alcuni telefoni per anziani (Super Bravo Plus, new Amico Gsm, i-Baby A88, Flip SAIET)
- Volkswagen: cosa fare se l'impostazione della sfera privata torna "Massima" e quindi i servizi in remoto vengono disattivati
- Come modificare o cancellare un beneficiario/contatto precedentemente salvato in un sito della banca (e.g. BNL, IngDirect, Intesa SanPaolo)
- Evento pubblico e gratuito "Quando si pranzava alle cinque del pomeriggio" di Alessandro Barbero al Museo del Risorgimento: come prenotare???
- TIM Power Famiglia Iron a 4,99€: un'ottima offerta... ma attenzione a disattivare per tempo le (subdole) opzioni attivate di default (gratuite solo per poco)!!
- Referendum e iniziative popolari: è possibile sottoscrivere la proposta di legge per la rivalutazione delle pensioni
- Tramite il sito della banca, impossibile pagare l'F24 per le tasse TEFA e 3944 (rifiuti) inserendo le informazioni così come indicate nel modulo fornito dal sito della SORIS: come risolvere
Foto su Flickr
Annunci




