Accueil > > > UTILISATION DES PROPERTYBAG: FICHIER DE CONFIGURATION
UTILISATION DES PROPERTYBAG: FICHIER DE CONFIGURATION
Information sur la source
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
Sources de la même categorie
Commentaires et avis
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 :<Item>
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
|
Derniers Blogs
TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 !TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 ! par ROMELARD Fabrice
Speakers: Fabrice Meillon et Stanislas Quastana Cette session est basée entièrement sur celle donnée lors de la BUILD cet hiver. Il n'y a pas d'ajout d'information en rapport avec cet évènement passé. Windows 8 Server sera intégralem...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [HTML5] AUTOUR DU W3C : NOUVEAUX STANDARDS ET WEB MOBILE (LILLE)[HTML5] AUTOUR DU W3C : NOUVEAUX STANDARDS ET WEB MOBILE (LILLE) par Gio
Je m'y prends un peu tard je sais, mais bon je suis développeur web et donc hyper fainéant ! Toujours dans le cadre des technologies émergentes, ici HTML5, parce qu'on aime HTML5 chez Wyg , nous seront présent, le vieux ( Aurélien V.) et moi, pour pr...
Cliquez pour lire la suite de l'article par Gio [WP7] DYNAMICALLY CHANGE STARTUP PAGE[WP7] DYNAMICALLY CHANGE STARTUP PAGE par KooKiz
Let's say that you want to allow the user to customize the startup page of your application. You can easily change the startup page by editing the 'NavigationPage' attribute in the manifest file. But the manifest cannot be modified once the applicatio...
Cliquez pour lire la suite de l'article par KooKiz SESSION SILVERLIGHT 5 3D : SLIDES ET DEMOSSESSION SILVERLIGHT 5 3D : SLIDES ET DEMOS par Groc
Durant les techdays, j'ai eu le plaisir d'animer une session sur Silverlight 5 et la 3D avec Simon Ferquel. Comme promis, voici nos slides et mes démos (celles avec le viper BSG) ici et là. Pour mémoire, les démos utilisent toutes le viper BSG...
Cliquez pour lire la suite de l'article par Groc
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|