begin process at 2012 02 13 14:52:42
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Divers

 > UTILISATION DES PROPERTYBAG: FICHIER DE CONFIGURATION

UTILISATION DES PROPERTYBAG: FICHIER DE CONFIGURATION


 Information sur la source

Note :
9 / 10 - par 4 personnes
9,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Divers Classé sous :propertybag, fichier, configuration Niveau :Expert Date de création :05/01/2000 Vu :6 342

Auteur : FlyKiller

Ecrire un message privé
Site perso
Commentaire sur cette source (1)
Ajouter un commentaire et/ou une note

 Description

Quand on doit sauver les options de configuration, on a deux choix: soit dans une DB (si le prg en utilise une), soit dans un fichier INI (trop lisible dans certains cas). Ce que je vous proprose est l'utilisation de l'objet PropertyBag qui n'est autre que celui utilisé quand on sauve les propriété d'un contrôle (.OCX) pendant le design-time. Sauver le contenu ci-dessous dans un fichier nommé config.cls

Source

  • VERSION 1.0 CLASS
  • BEGIN
  • MultiUse = -1 'True
  • Persistable = 0 'NotPersistable
  • DataBindingBehavior = 0 'vbNone
  • DataSourceBehavior = 0 'vbNone
  • MTSTransactionMode = 0 'NotAnMTSObject
  • END
  • Attribute VB_Name = "Config"
  • Attribute VB_GlobalNameSpace = False
  • Attribute VB_Creatable = True
  • Attribute VB_PredeclaredId = False
  • Attribute VB_Exposed = False
  • Option Explicit
  • Private colPName As Collection
  • Private colPValue As Collection
  • Private Sub Class_Initialize()
  • Set colPName = New Collection
  • Set colPValue = New Collection
  • End Sub
  • Private Sub Class_Terminate()
  • Set colPName = Nothing
  • Set colPValue = Nothing
  • End Sub
  • Public Property Get PropertyValue(ByVal PropertyName As String) As Variant
  • Dim I As Integer
  • I = PropertyIndex(PropertyName)
  • If I > 0 Then
  • PropertyValue = colPValue(I)
  • Else
  • PropertyValue = Null
  • End If
  • End Property
  • Public Property Let PropertyValue(ByVal PropertyName As String, ByVal NewValue As Variant)
  • Dim I As Integer
  • I = PropertyIndex(PropertyName)
  • If I > 0 Then
  • If IsNull(NewValue) = True Then
  • colPName.Remove I
  • colPValue.Remove I
  • Else
  • colPName.Add LCase$(Trim$(PropertyName))
  • colPValue.Add NewValue
  • End If
  • Else
  • If IsNull(NewValue) = False Then
  • colPName.Add LCase$(Trim$(PropertyName))
  • colPValue.Add NewValue
  • End If
  • End If
  • End Property
  • Public Function SaveFile(ByVal FileName As String) As Boolean
  • Dim PB As PropertyBag
  • Dim I As Integer
  • If Len(FileName) > 0 Then
  • Set PB = New PropertyBag
  • PB.WriteProperty "PropertyCount", colPName.Count
  • For I = 1 To colPName.Count
  • PB.WriteProperty "PropertyName" & I, colPName(I)
  • PB.WriteProperty "PropertyValue" & I, colPValue(I)
  • Next
  • On Error Resume Next
  • Kill FileName
  • On Error GoTo 0
  • Open FileName For Binary As #1
  • Put #1, , PB.Contents
  • Close #1
  • End If
  • SaveFile = True
  • End Function
  • Public Function LoadFile(ByVal FileName As String) As Boolean
  • Dim PB As PropertyBag
  • Dim I As Integer
  • 'Dim NbDtl As Integer
  • Dim vPB As Variant
  • Dim bPB() As Byte
  • 'Dim VD As CVerDetail
  • 'Dim Pwd As String
  • ClearCollection colPName
  • ClearCollection colPValue
  • If Len(FileName) > 0 Then
  • On Error GoTo ErrorOpen
  • Open FileName For Binary As #1
  • Get #1, , vPB
  • bPB = vPB
  • Close #1
  • Set PB = New PropertyBag
  • PB.Contents = bPB
  • For I = 1 To PB.ReadProperty("PropertyCount", 0)
  • colPName.Add PB.ReadProperty("PropertyName" & I, "")
  • colPValue.Add PB.ReadProperty("PropertyValue" & I, Null)
  • Next
  • LoadFile = True
  • Else
  • LoadFile = False
  • End If
  • Exit Function
  • ErrorOpen:
  • Close 1
  • LoadFile = False
  • Exit Function
  • End Function
  • Private Function PropertyExists(ByVal PropertyName As String) As Boolean
  • If PropertyIndex(PropertyName) > 0 Then PropertyExists = True
  • End Function
  • Private Function PropertyIndex(ByVal PropertyName As String) As Integer
  • Dim I As Integer
  • PropertyName = LCase$(Trim$(PropertyName))
  • For I = 1 To colPName.Count
  • If colPName(I) = PropertyName Then PropertyIndex = I: Exit Function
  • Next
  • End Function
  • Private Sub ClearCollection(ByRef Col As Collection)
  • While Col.Count > 0
  • Col.Remove 1
  • Wend
  • End Sub
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "Config"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private colPName As Collection
Private colPValue As Collection

Private Sub Class_Initialize()
  Set colPName = New Collection
  Set colPValue = New Collection
End Sub

Private Sub Class_Terminate()
  Set colPName = Nothing
  Set colPValue = Nothing
End Sub

Public Property Get PropertyValue(ByVal PropertyName As String) As Variant
Dim I As Integer

  I = PropertyIndex(PropertyName)
  If I > 0 Then
    PropertyValue = colPValue(I)
  Else
    PropertyValue = Null
  End If
End Property

Public Property Let PropertyValue(ByVal PropertyName As String, ByVal NewValue As Variant)
Dim I As Integer

  I = PropertyIndex(PropertyName)
  If I > 0 Then
    If IsNull(NewValue) = True Then
      colPName.Remove I
      colPValue.Remove I
    Else
      colPName.Add LCase$(Trim$(PropertyName))
      colPValue.Add NewValue
    End If
  Else
    If IsNull(NewValue) = False Then
      colPName.Add LCase$(Trim$(PropertyName))
      colPValue.Add NewValue
    End If
  End If
End Property

Public Function SaveFile(ByVal FileName As String) As Boolean
Dim PB As PropertyBag
Dim I As Integer

  If Len(FileName) > 0 Then
    Set PB = New PropertyBag
    PB.WriteProperty "PropertyCount", colPName.Count
    For I = 1 To colPName.Count
      PB.WriteProperty "PropertyName" & I, colPName(I)
      PB.WriteProperty "PropertyValue" & I, colPValue(I)
    Next
    On Error Resume Next
    Kill FileName
    On Error GoTo 0
    Open FileName For Binary As #1
    Put #1, , PB.Contents
    Close #1
  End If
  SaveFile = True
End Function

Public Function LoadFile(ByVal FileName As String) As Boolean
Dim PB As PropertyBag
Dim I As Integer
'Dim NbDtl As Integer
Dim vPB As Variant
Dim bPB() As Byte
'Dim VD As CVerDetail
'Dim Pwd As String

  ClearCollection colPName
  ClearCollection colPValue

  If Len(FileName) > 0 Then
    On Error GoTo ErrorOpen
    Open FileName For Binary As #1
    Get #1, , vPB
    bPB = vPB
    Close #1
    Set PB = New PropertyBag
    PB.Contents = bPB
    
    For I = 1 To PB.ReadProperty("PropertyCount", 0)
      colPName.Add PB.ReadProperty("PropertyName" & I, "")
      colPValue.Add PB.ReadProperty("PropertyValue" & I, Null)
    Next
    LoadFile = True
  Else
    LoadFile = False
  End If
  Exit Function

ErrorOpen:
  Close 1
  LoadFile = False
  Exit Function
End Function

Private Function PropertyExists(ByVal PropertyName As String) As Boolean
  If PropertyIndex(PropertyName) > 0 Then PropertyExists = True
End Function

Private Function PropertyIndex(ByVal PropertyName As String) As Integer
Dim I As Integer

  PropertyName = LCase$(Trim$(PropertyName))
  For I = 1 To colPName.Count
    If colPName(I) = PropertyName Then PropertyIndex = I: Exit Function
  Next
End Function

Private Sub ClearCollection(ByRef Col As Collection)
  While Col.Count > 0
    Col.Remove 1
  Wend
End Sub

 Conclusion

Voici un exemple d'utilisation. Mettez-le dans un module et n'oubliez pas de changer, dans le propriétés du projet, que le démarrage est la sub Main.


>>>>>>>> Début de sub Main >>>>>>>>>>>>>>>>>
Sub Main()
Dim X As Config

  Set X = New Config
  
  X.PropertyValue("DateTime") = Now
  X.PropertyValue("Version") = App.Major
  X.PropertyValue("SayHello") = "Hello"
  
  Debug.Print X.PropertyValue("DateTime")
  Debug.Print X.PropertyValue("Version")
  Debug.Print X.PropertyValue("SayHello")
  Debug.Print X.PropertyValue("Invalid")

  X.SaveFile "C:\Temp\TestFile.Cfg"
  
  Set X = Nothing
  Set X = New Config
  
  X.LoadFile "C:\Temp\TestFile.Cfg"
  
  Debug.Print X.PropertyValue("DateTime")
  Debug.Print X.PropertyValue("Version")
  Debug.Print X.PropertyValue("SayHello")
  Debug.Print X.PropertyValue("Invalid")
End Sub
<<<<<<<<<<<<<< Fin de Sub Main <<<<<<<<<<<<<<<<<


 Sources du même auteur

Source avec Zip FWDP: USER-DEFINED PROPERTIES
Source avec Zip CATALOGUER UN DISQUE: FILESYSTEMOBJECT VS. FONCTION API
Source avec Zip AUDITEZ VOS DONNÉES SUR SQL SERVER
Source avec Zip FICHIERS RESOURCE EXTERNES
Source avec Zip REGOCX: UN REGSVR32 EN VISUAL BASIC

 Sources de la même categorie

Source avec Zip TEXTBOX EN NUMÉRIQUE par 320C
Source avec Zip DÉCIMAL TO HEXDECIMAL par loulou27200
SOUS-TITRES : INCRÉMENTATION DE TOUTES LES CHAÎNES DE CARACT... par ALMIRA
Source avec Zip Source avec une capture EVALUER UN NOMBRE D'OBJETS AVEC UNE BALANCE ET DEUX ÉCHANTIL... par lexsty
Source avec Zip Source avec une capture PETIT LOGICIEL DE DEVIS SANS BD par lololilizozo

 Sources en rapport avec celle ci

Source avec Zip Source .NET (Dotnet) DÉFRAGMENTER UN FICHIER par ShareVB
Source avec Zip Source .NET (Dotnet) MODIFIER LES EXTENSION DES FICHIERS par okosa
Source avec Zip Source avec une capture FILE,SECURITY,FICHIER par okosa
Source avec Zip Source avec une capture Source .NET (Dotnet) PATCHEUR DE FICHIER par tototh
Source avec Zip REGISTRE POUR FICHIER DE CONFIGURATION par vbbreizh

Commentaires et avis

Commentaire de Chatmuel le 20/01/2005 10:16:11

Merci ça marche impecable !!

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Access 2000, lire un fichier texte comme fichier de configuration. [ par bs605124 ] J'ai un fichier texte qui sert de configuration :Ville = BruxellesCompteur = 2Stock = 3568A l'ouverture d'access, comment mettre ces 3 données dans de Fichier de configuration d'application [ par Ralph29 ] Bjr à tous...Je sui développeur depuis peu, alors soyez indulgent face à ma grande ignorance face à ce problème qui peut paraitre simple mais qui est Fichier de configuration en VB.NET (controle dynamique) [ par Romuald76 ] Salut à tous,Voila je développe une application et j'aurais besoin de stocker des valeurs comme par exemple un nom de connexion. Chaque utilisateur pe app.config [ par Sipat ] Bonsoir,J'aimerai savoir si le fichier app.config et obligatoire avec le fichier générer ? Voilà mon fichier si je l'enleve il se produit une erreur e fichier de configuration [ par SIKOV ] bonjourje voudrais savoir deux choses:1) Comment créer un fichier de configuration pour une application 2) Comment à partir d'un CD je peux transférer COnfiguration fichier xml [ par Markus971 ] Bonjour, je débute avec les fichies xml et je voudrais savoir comment quel valeur correspond le type de donnée booléen. voici le code :&lt;Item&gt;    Utilisation fichier de configuration [ par MacWarrior ] Bonjour à tous, Depuis un bon moment maintenant, je cherche à utiliser un fichier de configuration de ce type :  // Mon Commentaire //Nom_du_paramètre Charger une valuer dans une combobox depuis un fichier texte [ par patmtp35 ] voila Je suis novice j'a presque finit mon GUI pour créer un fichier de configuration pour un de mes dev. mais je bloque sur un point. Avec ce GUI Problème pour renommer un fichier avant de l'envoyer par mail [ par mistermail ] Bonjour, Voila j'aimerais renommer les fichiers présents dans un répertoire avant de les envoyer par mail. Je ne vois pas où se trouve le prob Fichier de configuration [ par AirByte ] Bonjour a tous J'aurais besoin de conseils, j'ai une application qui nécessite un fichier de configuration modifiable dans le programme et autant que


Nos sponsors


Sondage...

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 2,590 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales