Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to insert blank lines between sections of INI file? 1

Status
Not open for further replies.

Sheco

Programmer
Jan 3, 2005
5,457
US
I have an application that saves various settings to an INI file. I like for the file to have a blank line between each INI section. Like this:

[Section1]
key1=val1
key2=val2

[Section2]
key1=val 1


My current technique for accomplishing this is to read the entire contents of the file into a string and use:
Replace(strFile, (Chr(10) & "["), (Chr(10) & vbCrLf & "["))
Then I overwrite the file with strFile.

This works fine but it is really offensive to my sense of, um, well I don't like it. Surely there is a more graceful way to do this!
Any ideas?
 
Use WritePrivateProfileSection, and cheat...
Code:
[blue]Option Explicit
Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long

Private Sub Command1_Click()
    Dim strKeys As String
    Dim strSection As String
    
    strSection = "Section1"
    strKeys = "key1=val1" & Chr(0) & "key2=val2" & Chr(0) & vbCr & Chr(0)
    WritePrivateProfileSection strSection, strKeys, "c:\Test.ini"
    
    strSection = "Section2"
    strKeys = "key1=val1" & Chr(0) & vbCr & Chr(0)
    WritePrivateProfileSection strSection, strKeys, "c:\Test.ini"
End Sub[/blue]
 
Hahahaha!

I like it a lot better than what I was doing before.

A star for you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top