Read the content of a .ini file
thierry jupiter
-
wida Posted messages 170 Status Member -
wida Posted messages 170 Status Member -
Bonjour,
I would like to read the content of a .ini file with VB
read line by line in a block
read the line word by word
would you have some code to suggest to me
thank you!
I would like to read the content of a .ini file with VB
read line by line in a block
read the line word by word
would you have some code to suggest to me
thank you!
5 answers
-
read an ini file:
dim lgn as string
dim Vect
open "file.ini" for input as #1 'Opening the file for reading
line input #1,lgn 'lgn contains an entire line up to the vbcrlf character
...
'if you want to split word by word you do
vect=split(replace(lgn,"="," "), " ")
...
reset 'Closes all open files
lgn gives you a key=value line
vect will give you an array containing all the words from the line. -
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
Function LireINI(Entete As String, Variable As String) As String
Dim Retour As String
Fichier = Path & "\" & FileName & ".ini"
Retour = String(255, Chr(0))
LireINI = Left$(Retour, GetPrivateProfileString(Entete, ByVal Variable, "", Retour, Len(Retour), Fichier))
End Function
Function EcrireINI(Entete As String, Variable As String, Valeur As String) As String
Fichier = Path & "\" & FileName & ".ini"
WriteINI = WritePrivateProfileString(Entete, Variable, Valeur, Fichier)
End Function
' Pour l'executer ex :
EcrireINI("MonEntete", "MaVariable", "MaValeur")
LireINI("MonEntete", "MaVariable") -
Jamil BETTAIEB :
Hello,
I applied it to an Excel workbook and it works very well
Below is the modified code:
-----------------------------------------------------------
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
Public Function LireINI(Entete As String, Variable As String, Path As String, Filename As String) As String
Dim Retour As String
Fichier = Path & "\" & Filename & ".ini"
Retour = String(255, Chr(0))
LireINI = Left$(Retour, GetPrivateProfileString(Entete, ByVal Variable, "", Retour, Len(Retour), Fichier))
End Function
Public Function EcrireINI(Entete As String, Variable As String, Valeur As String, Path As String, Filename As String) As String
Fichier = Path & "\" & Filename & ".ini"
WriteINI = WritePrivateProfileString(Entete, Variable, Valeur, Fichier)
End Function
Usage:
-----------------------
Private Sub CommandButton1_Click()
Dim Temp As String
Dim MonEntete As String
Dim Var1 As String
Dim Valeur1 As String
MonEntete = "Remote File Processing Parameters"
Var1 = "File Name"
'---Write the values to be put in the INI file---
'Valeur1 = "CA_QUOTIDIEN_07_0074.xls"
'Temp = EcrireINI(MonEntete, Var1, Valeur1, ActiveWorkbook.Path, "AppConf1")
'---Write the values to be put in the INI file---
Valeur1 = ""
Valeur1 = LireINI(MonEntete, Var1, ActiveWorkbook.Path, "AppConf1")
MsgBox Valeur1
End Sub -
Nice job,
Thanks a lot
--
The dream leads to nothing, action leads to everything.
(J. FIAUX). -