INI file help

  • 14 years ago

    i am using the GetPrivateProfileString and WritePrivateProfileString to a INI file using VB6 what i would like to know is anybody know how to delete a zone from an INI file?

    Example

    [Zone1]

    Test1=Data

    Test2=Data

    i would like to delete Zone1 and everything contained in that zone.Thanks

  • 14 years ago

    If you want to use API to solve the problem, actually function WritePrivateProfileSection could do it. Just set the second parameter value as empty string ( not null).

    For example:

    1.Declare API function:
    Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long

    2.Add a command named "Command1", then add event as:
    Private Sub Command1_Click()
         WritePrivateProfileSection "Zone1", "", "my.INI" 
    End Sub


    It will delete all the items which was under [Zone1], but will not delete "[Zone1]"

    If you even want to delete the section [Zone], then you can write as(Different Declare):

    Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As Any, ByVal lpFileName As String) As Long 
         
    Private Sub Command1_Click()
         WritePrivateProfileSection "Zone1", 0&, "my.INI" 
    End Sub



     

  • 14 years ago

    wintermeng

       Thanks for the info that does exactly what i want it to do. Your input will save me a ton of time trying to code what i wanted to do. So thanks again.

  • 14 years ago
    One more question if i wanted to delete a specific key for a section but not all the keys in that section how would that be done?
  • 14 years ago

    i believe the function is called WritePrivateProfileString, but I haven't used INIs for a while so srry!

    The MSDN is a good source of documentation as Microsoft create the programming tools.

  • 14 years ago

    Hi, man, I just forward following code to you, wishing it can help you.

     

    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[separator]
    '---------------------------------------------------------------------------------
    Public Function GetIniStr(ByVal AppName As String, ByVal In_Key As String, ByVal FileName As String) As String
      Dim GetStr As String
      
      On Error GoTo GetIniStrErr
      
      If VBA.Trim(In_Key) = "" Then
        GoTo GetIniStrErr
      End If

      GetStr = VBA.String(128, 0)
      GetPrivateProfileString AppName, In_Key, "", GetStr, 256, FileName    'App.Path & "\SourceDB.ini"
      GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
      If GetStr = "" Then
        GoTo GetIniStrErr
      Else
        GetIniStr = GetStr
        GetStr = ""
      End If
      
    Exit Function
    GetIniStrErr:
      Err.Clear
      GetIniStr = ""
      GetStr = ""
    End Function
    '----------------------------------------------------------------------------------
    Public Function WriteIniStr(ByVal AppName As String, ByVal In_Key As String, ByVal In_Data As String, ByVal FileName As String) As Boolean
      On Error GoTo WriteIniStrErr
      WriteIniStr = True
      If VBA.Trim(In_Key) = "" Or VBA.Trim(AppName) = "" Then
        GoTo WriteIniStrErr
      Else
        WritePrivateProfileString AppName, In_Key, In_Data, FileName
      End If
      Exit Function
    WriteIniStrErr:
      Err.Clear
      WriteIniStr = False
    End Function











































Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Walking on water and developing software from a specification are easy if both are frozen.” - Edward V Berard