Accueil > > > SAVESETTING ET GETSETTING DANS UN FICHIER INI
SAVESETTING ET GETSETTING DANS UN FICHIER INI
Information sur la source
Description
Ce code est à copier dans un Module. Il remplace les fonctions Vb SaveSetting,GetSetting,GetAllSetting,DeleteSetting en gardant la même syntaxe. l'appel au anciennes fonction est toujours possible avec la syntaxe Vba.GetSetting , Vba.SaveSetting etc . Une petite limitation tout de même avec GetAllSettings : la variable qui recevra le tableau devrat être un variant à l'exception de tout autre type y compris untableau Exemple : Dim Temp as Variant Temp = GetAllSettings(App.Title, "General")
Source
- Option Explicit
-
- Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
- Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
- Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
-
-
-
-
- Public Sub SaveSetting(AppName As String, Section As String, Key As String, Setting As String)
- Dim lRet As Long
- Dim Path As String
- Path = App.Path
- If Right(Path, 1) <> "\" Then
- Path = Path & "\"
- End If
- lRet = WritePrivateProfileString(Section, Key, Setting, Path & AppName & ".ini")
- End Sub
- Public Function GetSetting(AppName As String, Section As String, Key As String, Optional Default As String) As String
- Dim lRet As Long
- Dim Path As String
- Dim strTemp As String
- strTemp = Space(32567)
- Path = App.Path
- If Right(Path, 1) <> "\" Then
- Path = Path & "\"
- End If
- lRet = GetPrivateProfileString(Section, Key, Default, strTemp, Len(strTemp), Path & AppName & ".ini")
- lRet = InStr(strTemp, Chr$(0))
- If lRet = 0 Then
- GetSetting = ""
- Else
- GetSetting = Left(strTemp, lRet - 1)
- End If
- End Function
- Public Function GetAllSettings(AppName As String, Section As String) As Variant
- Dim lRet As Long
- Dim Path As String
- Dim strTemp As String
- Dim Table() As String
- Dim Table2() As String
- Dim iPnt As Integer
- Dim iPnt2 As Integer
- Dim iPosit As Integer
- strTemp = Space(32567)
- Path = App.Path
- If Right(Path, 1) <> "\" Then
- Path = Path & "\"
- End If
-
- lRet = GetPrivateProfileSection(Section, strTemp, Len(strTemp), Path & AppName & ".ini")
- iPnt = 0
- 'For Redim+Preserve tables only the las index can be changed
- If Left(strTemp, 2) = Chr$(0) & Chr$(0) Then
- Exit Function
- End If
- Do While Left(strTemp, 1) <> Chr$(0)
- ReDim Preserve Table(1, iPnt)
- iPosit = InStr(strTemp, "=")
- Table(0, iPnt) = Left$(strTemp, iPosit - 1)
- strTemp = Mid$(strTemp, iPosit + 1)
- iPosit = InStr(strTemp, Chr$(0))
- Table(1, iPnt) = Left$(strTemp, iPosit - 1)
- strTemp = Mid$(strTemp, iPosit + 1)
- iPnt = iPnt + 1
- Loop
- ReDim Table2(iPnt - 1, 1)
- For iPnt2 = 0 To iPnt - 1
- Table2(iPnt2, 0) = Table(0, iPnt2)
- Table2(iPnt2, 1) = Table(1, iPnt2)
- Next
- GetAllSettings = Table2
- End Function
- Public Function DeleteSetting(AppName As String, Section As String, Optional Key As String)
- Dim lRet As Long
- Dim Path As String
- Path = App.Path
- If Right(Path, 1) <> "\" Then
- Path = Path & "\"
- End If
- If Key = "" Then
- lRet = WritePrivateProfileString(Section, vbNullString, vbNullString, Path & AppName & ".ini")
- Else
- lRet = WritePrivateProfileString(Section, Key, vbNullString, Path & AppName & ".ini")
- End If
- End Function
Option Explicit
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Sub SaveSetting(AppName As String, Section As String, Key As String, Setting As String)
Dim lRet As Long
Dim Path As String
Path = App.Path
If Right(Path, 1) <> "\" Then
Path = Path & "\"
End If
lRet = WritePrivateProfileString(Section, Key, Setting, Path & AppName & ".ini")
End Sub
Public Function GetSetting(AppName As String, Section As String, Key As String, Optional Default As String) As String
Dim lRet As Long
Dim Path As String
Dim strTemp As String
strTemp = Space(32567)
Path = App.Path
If Right(Path, 1) <> "\" Then
Path = Path & "\"
End If
lRet = GetPrivateProfileString(Section, Key, Default, strTemp, Len(strTemp), Path & AppName & ".ini")
lRet = InStr(strTemp, Chr$(0))
If lRet = 0 Then
GetSetting = ""
Else
GetSetting = Left(strTemp, lRet - 1)
End If
End Function
Public Function GetAllSettings(AppName As String, Section As String) As Variant
Dim lRet As Long
Dim Path As String
Dim strTemp As String
Dim Table() As String
Dim Table2() As String
Dim iPnt As Integer
Dim iPnt2 As Integer
Dim iPosit As Integer
strTemp = Space(32567)
Path = App.Path
If Right(Path, 1) <> "\" Then
Path = Path & "\"
End If
lRet = GetPrivateProfileSection(Section, strTemp, Len(strTemp), Path & AppName & ".ini")
iPnt = 0
'For Redim+Preserve tables only the las index can be changed
If Left(strTemp, 2) = Chr$(0) & Chr$(0) Then
Exit Function
End If
Do While Left(strTemp, 1) <> Chr$(0)
ReDim Preserve Table(1, iPnt)
iPosit = InStr(strTemp, "=")
Table(0, iPnt) = Left$(strTemp, iPosit - 1)
strTemp = Mid$(strTemp, iPosit + 1)
iPosit = InStr(strTemp, Chr$(0))
Table(1, iPnt) = Left$(strTemp, iPosit - 1)
strTemp = Mid$(strTemp, iPosit + 1)
iPnt = iPnt + 1
Loop
ReDim Table2(iPnt - 1, 1)
For iPnt2 = 0 To iPnt - 1
Table2(iPnt2, 0) = Table(0, iPnt2)
Table2(iPnt2, 1) = Table(1, iPnt2)
Next
GetAllSettings = Table2
End Function
Public Function DeleteSetting(AppName As String, Section As String, Optional Key As String)
Dim lRet As Long
Dim Path As String
Path = App.Path
If Right(Path, 1) <> "\" Then
Path = Path & "\"
End If
If Key = "" Then
lRet = WritePrivateProfileString(Section, vbNullString, vbNullString, Path & AppName & ".ini")
Else
lRet = WritePrivateProfileString(Section, Key, vbNullString, Path & AppName & ".ini")
End If
End Function
Conclusion
Un fois que vous avez créé le module, il suffit de l'ajouter à un code existant pour que les save/getsetting et tutiquanti de votre projet ecrivent dans un fichier ini plutot que dans Base de registre
J'espère que cela aideras ceux qui comme moi sont nostalgique des fichiers ini
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
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 [TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES par gpommier
Suite à la session que j'ai présenté sur WebMatrix 2, vous pouvez trouver les slides ici, ainsi que les démos en packages nuget : démos1 et démos2 J'en profite pour remercier chaleureusement tous ceux qui sont venus très nombreux à cette sess...
Cliquez pour lire la suite de l'article par gpommier [SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE !MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE ! par Vko
Hier durant une session dédiée aux Techdays 2012, j'ai eu le plaisir d'annoncer la sortie de la Béta 2 de Mishra Reader. C'est quoi ? Pour les utilisateurs, c'est une vraie expérience de lecture de flux RSS sur Windows. Rien à voir avec les produit...
Cliquez pour lire la suite de l'article par Vko
Logiciels
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 Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning
|