Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2557 connectés 

  FORUM HardWare.fr
  Programmation
  C#/.NET managed

  Enregistrer les paramètres d'un fichier de configuration

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Enregistrer les paramètres d'un fichier de configuration

n°1657716
haazheel
Posté le 13-12-2007 à 14:08:27  profilanswer
 

Salut à tous,
 
j'ai un souci concernant les fichiers de configuration.
 
J'ai le fichier de config suivant:
 

Code :
  1. <configuration>
  2.   <configSections>
  3.     <section name="SQLSection" type="GilInterimStatsViewer.SQLSectionHandler, GilInterimStatsViewer" />
  4.     <section name="ExportImageSection" type="GilInterimStatsViewer.ExportImageSectionHandler, GilInterimStatsViewer" />
  5.   </configSections>
  6.   <SQLSection type="GilInterimStatsViewer.SQLSection, GilInterimStatsViewer">
  7.     <Server>xxx</Server>
  8.     <DBName>xxx</DBName>
  9.   </SQLSection>
  10.   <ExportImageSection type="GilInterimStatsViewer.ExportImageSection, GilInterimStatsViewer">
  11.     <ImageFormat>image/png</ImageFormat>
  12.     <ImageCompression>100</ImageCompression>
  13.     <ImageWidth>1280</ImageWidth>
  14.   </ExportImageSection>
  15. </configuration>


J'ai créé les classes qui vont bien pour la lecture des paramètres des nouvelles sections:
 

Code :
  1. using System;
  2. using System.Configuration;
  3. using System.Xml;
  4. using System.Xml.Serialization;
  5. namespace GilInterimStatsViewer
  6. {
  7.     public class SQLSection
  8.     {
  9.         private string ServerName_;
  10.         private string DBName_;
  11.         public string Server
  12.         {
  13.             get { return ServerName_; }
  14.             set { ServerName_ = value; }
  15.         }
  16.         public string DBName
  17.         {
  18.             get { return DBName_; }
  19.             set { DBName_ = value; }
  20.         }
  21.     }
  22.     class SQLSectionHandler : IConfigurationSectionHandler
  23.     {
  24.         public object Create(object parent, object configContext, XmlNode section)
  25.         {
  26.             XmlSerializer xs = new XmlSerializer(typeof(SQLSection));
  27.             XmlNodeReader xnr = new XmlNodeReader(section);
  28.             return xs.Deserialize(xnr);
  29.         }
  30.     }
  31. }


J'arrive à lire correctement les valeurs:
 

Code :
  1. SQLSection sqlSect = (SQLSection)ConfigurationManager.GetSection("SQLSection" );
  2. string server = sqlSect.Server;
  3. string db = sqlSect.DBName;


Maintenant, problème, j'arrive à enregistrer de nouvelles valeurs pour ces sections personalisées...
 
Comment je dois faire?
 
J'ai essayé avec ça:
 

Code :
  1. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);


Mais je m'en sors pas pour le moment...
 
Qui peut me filer un petit coup de main?
 
Merci d'avance
 
Mike

mood
Publicité
Posté le 13-12-2007 à 14:08:27  profilanswer
 

n°1659281
Xas
Freshershist
Posté le 17-12-2007 à 11:47:16  profilanswer
 

Ben heu... J'ai du rater quelque chose...
config.Save(); ?


---------------
H. - 48h en fiat et j'ai déjà perdu la maitrise de mon argent

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C#/.NET managed

  Enregistrer les paramètres d'un fichier de configuration

 

Sujets relatifs
faire lire un fichier audio grace à l'assembleur pb de programmationRemplacement chaine de caractères dans fichier
script pour renommer un fichierExplorateur de fichier Python
[batch DOS] boucle if qui ne trouve pas le fichierreadfile sur https avec un gros fichier
ouverture fichier texte dans une boucle //ex edit meta tagsMacro appelant un fichier .xls annexe -> Pb d'accès aux données
enregistrer une page generer par php en un fichier texte.ref 
Plus de sujets relatifs à : Enregistrer les paramètres d'un fichier de configuration


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR