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

  1. using System;
  2. using System.Windows.Forms;
  3. using System.Xml;
  4. using be.gaudry.observer;
  5.  
  6. namespace be.gaudry.model.config
  7. {
  8. public class Settings
  9. {
  10. #region declarations and constructors
  11. private XmlDocument xmlDocument;
  12. private string documentPath;
  13. private bool hidden;
  14. /// <summary>
  15. /// Provides method to store settings in a hidden xml file in the application directory
  16. /// </summary>
  17. public Settings()
  18. : this(Application.StartupPath + "\\settings.xml",true)
  19. {
  20. }
  21. /// <summary>
  22. /// Provides method to store settings in a xml file
  23. /// </summary>
  24. /// <param name="documentPath">Path of the xml file</param>
  25. /// <param name="hidden">True if the file must be hidden</param>
  26. public Settings(string documentPath, bool hidden)
  27. {
  28. this.documentPath = documentPath;
  29. this.hidden = hidden;
  30. xmlDocument = new XmlDocument();
  31. initDocument();
  32. }
  33. #endregion
  34.  
  35. #region initializations
  36. /// <summary>
  37. /// Loads document if file is found, or create a new xml document in memory.
  38. /// </summary>
  39. private void initDocument()
  40. {
  41. try
  42. {
  43. hide(false);
  44. xmlDocument.Load(documentPath);
  45. hide(true);
  46. }
  47. catch { xmlDocument.LoadXml("<settings></settings>"); }
  48. }
  49. #endregion
  50.  
  51. #region public methods and properties
  52. public String SettingsPath
  53. {
  54. get
  55. {
  56. return documentPath;
  57. }
  58. set
  59. {
  60. documentPath = value;
  61. initDocument();
  62. }
  63. }
  64.  
  65. /// <summary>
  66. /// Saves all settings
  67. /// </summary>
  68. public void save()
  69. {
  70. try
  71. {
  72. hide(false);
  73. xmlDocument.Save(documentPath);
  74. hide(true);
  75. }
  76. catch (Exception e)
  77. {
  78. StaticObservable.notify(new Notification(Notification.VERBOSE.lowError, e, this));
  79. }
  80. }
  81. /// <summary>
  82. /// Modify the file attribute to hide the settings file
  83. /// </summary>
  84. private void hide(bool hide)
  85. {
  86. if (hidden && System.IO.File.Exists(documentPath))
  87. {
  88. try
  89. {
  90. if (!hide)//((System.IO.File.GetAttributes(documentPath) & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden)
  91. {
  92. // Show the file.
  93. System.IO.File.SetAttributes(documentPath, System.IO.FileAttributes.Archive);
  94. }
  95. else
  96. {
  97. // Hide the file.
  98. System.IO.File.SetAttributes(documentPath, System.IO.File.GetAttributes(documentPath) | System.IO.FileAttributes.Hidden);
  99. }
  100.  
  101. }
  102. catch { }
  103. }
  104. }
  105.  
  106. #region int value
  107. //todo : use tryparse instead ?
  108. public int getSetting(string xPath, int defaultValue)
  109. { return Convert.ToInt16(getSetting(xPath, Convert.ToString(defaultValue))); }
  110.  
  111. /// <summary>
  112. /// Sets an int value in the setting file.
  113. /// </summary>
  114. /// <param name="xPath">xml node path (slashes separated nodes)</param>
  115. /// <param name="value">value to set</param>
  116. /// <param name="save">true to save this value. Otherwise, you can set all values and call once the <code>save()</code> method.</param>
  117. public void putSetting(string xPath, int value, bool save)
  118. { putSetting(xPath, Convert.ToString(value), save); }
  119.  
  120. #endregion
  121.  
  122. #region bool value
  123. public bool getSetting(string xPath, bool defaultValue)
  124. { return "true".Equals(getSetting(xPath, (defaultValue) ? "true" : "false")); }
  125.  
  126. /// <summary>
  127. /// Sets a bool value in the setting file.
  128. /// </summary>
  129. /// <param name="xPath">xml node path (slashes separated nodes)</param>
  130. /// <param name="value">value to set</param>
  131. /// <param name="save">true to save this value. Otherwise, you can set all values and call once the <code>save()</code> method.</param>
  132. public void putSetting(string xPath, bool value, bool save)
  133. { putSetting(xPath, (value) ? "true" : "false", save); }
  134. #endregion
  135.  
  136. #region string value (default)
  137. public string getSetting(string xPath, string defaultValue)
  138. {
  139. XmlNode xmlNode = xmlDocument.SelectSingleNode("settings/" + xPath);
  140. if (xmlNode != null) { return xmlNode.InnerText; }
  141. else { return defaultValue; }
  142. }
  143. /// <summary>
  144. /// Sets a value in the setting file.
  145. /// </summary>
  146. /// <param name="xPath">xml node path (slashes separated nodes)</param>
  147. /// <param name="value">value to set</param>
  148. /// <param name="save">true to save this value. Otherwise, you can set all values and call once the <code>save()</code> method.</param>
  149. public void putSetting(string xPath, string value, bool save)
  150. {
  151. try
  152. {
  153. XmlNode xmlNode = xmlDocument.SelectSingleNode("settings/" + xPath);
  154. if (xmlNode == null) { xmlNode = createMissingNode("settings/" + xPath); }
  155. xmlNode.InnerText = value;
  156. if (save)
  157. {
  158. hide(false);
  159. xmlDocument.Save(documentPath);
  160. hide(true);
  161. }
  162. }
  163. catch (System.IO.DirectoryNotFoundException dnfe)
  164. {
  165. StaticObservable.notify(new Notification(Notification.VERBOSE.lowError, dnfe, this));
  166. }
  167. }
  168. #endregion
  169.  
  170. #endregion
  171.  
  172. #region private methods
  173. private XmlNode createMissingNode(string xPath)
  174. {
  175. string[] xPathSections = xPath.Split('/');
  176. string currentXPath = "";
  177. XmlNode testNode = null;
  178. XmlNode currentNode = xmlDocument.SelectSingleNode("settings");
  179. foreach (string xPathSection in xPathSections)
  180. {
  181. currentXPath += xPathSection;
  182. testNode = xmlDocument.SelectSingleNode(currentXPath);
  183. if (testNode == null) { currentNode.InnerXml += "<" + xPathSection + "></" + xPathSection + ">"; }
  184. currentNode = xmlDocument.SelectSingleNode(currentXPath);
  185. currentXPath += "/";
  186. }
  187. return currentNode;
  188. }
  189. #endregion
  190. }
  191. }

Structure et Fichiers du projet

Afficher/masquer...


Répertoires contenus dans /var/www/bin/sniplets/bibliobrol/broldev/src/model/config/ 
IcôneNomTailleModification
Pas de sous-répertoires.
IcôneNomTailleModification
| _ Répertoire parent0 octets1732360898 23/11/2024 12:21:38
Fichiers contenus dans /var/www/bin/sniplets/bibliobrol/broldev/src/model/config/ 
IcôneNomTailleModificationAction
IcôneNomTailleModificationAction
Afficher le fichier .cs|.csPluginsManager.cs3.8 Ko31/10/2018 18:33:08-refusé-
Afficher le fichier .cs|.csSource.cs2.8 Ko31/10/2018 18:33:08-refusé-
Afficher le fichier .cs|.csSettings.cs6.91 Ko31/10/2018 18:33:08-refusé-
Afficher le fichier .cs|.csPlugin.cs3.36 Ko31/10/2018 18:33:08-refusé-
Afficher le fichier .cs|.csSourcesManager.cs1.38 Ko31/10/2018 18:33:08-refusé-

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.