Settings.cs
Description du code
Settings.cs est un fichier du projet BrolDev.Ce fichier est situé dans /var/www/bin/sniplets/bibliobrol/broldev/src/.
Projet BrolDev : Librairie de composants réutilisables pour les applications BrolDev en CSharp.
Code source ou contenu du fichier
Code c# (Settings.cs) (191 lignes)
using System; using System.Windows.Forms; using System.Xml; using be.gaudry.observer; namespace be.gaudry.model.config { public class Settings { #region declarations and constructors private XmlDocument xmlDocument; private string documentPath; private bool hidden; /// <summary> /// Provides method to store settings in a hidden xml file in the application directory /// </summary> public Settings() : this(Application.StartupPath + "\\settings.xml",true) { } /// <summary> /// Provides method to store settings in a xml file /// </summary> /// <param name="documentPath">Path of the xml file</param> /// <param name="hidden">True if the file must be hidden</param> public Settings(string documentPath, bool hidden) { this.documentPath = documentPath; this.hidden = hidden; initDocument(); } #endregion #region initializations /// <summary> /// Loads document if file is found, or create a new xml document in memory. /// </summary> private void initDocument() { try { hide(false); xmlDocument.Load(documentPath); hide(true); } catch { xmlDocument.LoadXml("<settings></settings>"); } } #endregion #region public methods and properties public String SettingsPath { get { return documentPath; } set { documentPath = value; initDocument(); } } /// <summary> /// Saves all settings /// </summary> public void save() { try { hide(false); xmlDocument.Save(documentPath); hide(true); } catch (Exception e) { } } /// <summary> /// Modify the file attribute to hide the settings file /// </summary> private void hide(bool hide) { if (hidden && System.IO.File.Exists(documentPath)) { try { if (!hide)//((System.IO.File.GetAttributes(documentPath) & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden) { // Show the file. System.IO.File.SetAttributes(documentPath, System.IO.FileAttributes.Archive); } else { // Hide the file. System.IO.File.SetAttributes(documentPath, System.IO.File.GetAttributes(documentPath) | System.IO.FileAttributes.Hidden); } } catch { } } } #region int value //todo : use tryparse instead ? public int getSetting(string xPath, int defaultValue) { return Convert.ToInt16(getSetting(xPath, Convert.ToString(defaultValue))); } /// <summary> /// Sets an int value in the setting file. /// </summary> /// <param name="xPath">xml node path (slashes separated nodes)</param> /// <param name="value">value to set</param> /// <param name="save">true to save this value. Otherwise, you can set all values and call once the <code>save()</code> method.</param> public void putSetting(string xPath, int value, bool save) { putSetting(xPath, Convert.ToString(value), save); } #endregion #region bool value public bool getSetting(string xPath, bool defaultValue) { return "true".Equals(getSetting(xPath, (defaultValue) ? "true" : "false")); } /// <summary> /// Sets a bool value in the setting file. /// </summary> /// <param name="xPath">xml node path (slashes separated nodes)</param> /// <param name="value">value to set</param> /// <param name="save">true to save this value. Otherwise, you can set all values and call once the <code>save()</code> method.</param> public void putSetting(string xPath, bool value, bool save) { putSetting(xPath, (value) ? "true" : "false", save); } #endregion #region string value (default) public string getSetting(string xPath, string defaultValue) { XmlNode xmlNode = xmlDocument.SelectSingleNode("settings/" + xPath); if (xmlNode != null) { return xmlNode.InnerText; } else { return defaultValue; } } /// <summary> /// Sets a value in the setting file. /// </summary> /// <param name="xPath">xml node path (slashes separated nodes)</param> /// <param name="value">value to set</param> /// <param name="save">true to save this value. Otherwise, you can set all values and call once the <code>save()</code> method.</param> public void putSetting(string xPath, string value, bool save) { try { XmlNode xmlNode = xmlDocument.SelectSingleNode("settings/" + xPath); if (xmlNode == null) { xmlNode = createMissingNode("settings/" + xPath); } xmlNode.InnerText = value; if (save) { hide(false); xmlDocument.Save(documentPath); hide(true); } } catch (System.IO.DirectoryNotFoundException dnfe) { } } #endregion #endregion #region private methods private XmlNode createMissingNode(string xPath) { string[] xPathSections = xPath.Split('/'); string currentXPath = ""; XmlNode testNode = null; XmlNode currentNode = xmlDocument.SelectSingleNode("settings"); foreach (string xPathSection in xPathSections) { currentXPath += xPathSection; testNode = xmlDocument.SelectSingleNode(currentXPath); if (testNode == null) { currentNode.InnerXml += "<" + xPathSection + "></" + xPathSection + ">"; } currentNode = xmlDocument.SelectSingleNode(currentXPath); currentXPath += "/"; } return currentNode; } #endregion } }
Structure et Fichiers du projet
Afficher/masquer...Icône | Nom | Taille | Modification |
Pas de sous-répertoires. | |||
Icône | Nom | Taille | Modification |
| _ | Répertoire parent | 0 octets | 1732370418 23/11/2024 15:00:18 |
Utilisation de l'explorateur de code
- Navigation :
- Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
- Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
- La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
- Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
- Affichage :
- Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
- Actions :
- Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 16/10/2009, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/cs-broldev-source-rf-model/config//Settings.cs.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.