Read the content of a .ini file

thierry jupiter -  
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!

5 answers

  1. xthorx_be Posted messages 149 Status Member 131
     
    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.
    1
  2. anasweb
     
    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")
    1
  3. jamil1976
     
    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
    0
    1. tirakawa
       
      excellent
      thank you jamil
      0
  4. wida Posted messages 170 Status Member 17
     
    Nice job,
    Thanks a lot
    --
    The dream leads to nothing, action leads to everything.
    (J. FIAUX).
    0