Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
The following code is supposed to write a section in an ini-file.

Microsoft Help file states "WritePrivateProfileSection deletes the existing keys and values for the named section and inserts the key names and values in the buffer pointed to by the lpString parameter."
VB
Public Function WriteIniSection(ByVal Section As String, ByVal Values() As String, ByVal IniPath As String) As Boolean
   Dim s As String = ""
   For Each a As String In Values
      If a <> "" Then s &= a & ControlChars.NullChar
   Next
   s &= ControlChars.NullChar
   Return WritePrivateProfileSection(Section, s, IniPath) = 0
End Function

Except it doesn't overwrite - it appends.
I am using Windows7 64bit Framework 3.5. Is this why?
Posted

You are correct it does append.

did you find a solution to get it to overwrite ?
 
Share this answer
 
It could be because of 64bit windows but I doubt it is because of .NET. This is because these are API calls into kernel32.dll and are handled outside of .NET (unmanaged code). I.E. The component responsible for actually writing to the INI file is outside of the scope of .NET.

There is probably some wonky way to force an overwite just like there is "special" behaviour for passing null values to WritePrivateProfileString etc...
 
Share this answer
 
My solution is to treat the ini files as ordinary files and parse them etc.
 
Share this answer
 
No, it's not because of 64-bit Windows or the .NET Framework.

I don't know why it's not working, but those .INI functions are only there for backwards compatibility. Why are you using .INI files in your app? They are an outdated concept. I would suggest using .XML files instead, if at all possible.
 
Share this answer
 
Comments
Stuart Nathan 24-May-11 7:34am    
Because ini files caqn be easily read by Notepad and editted.
XML files are not so easy to edit, and neither is the Windows Registry which Microsoft recommend.
Dave Kreskowiak 24-May-11 7:51am    
What does your Delcare statement look like for WritePrivateProfileSection?
Stuart Nathan 24-May-11 7:56am    
Private Declare Ansi Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal Section As String, ByVal Text As String, ByVal FileName As String) As Int32
Dave Kreskowiak 24-May-11 8:00am    
Hmmm... it should be working. I can't test anything until I get to work.
Dave Kreskowiak 24-May-11 10:54am    
I don't know what you're doing, but it's working as expected on .NET 2.0, 3.0, 3.5, and 4.0 as both x86 and AnyCPU on a Win7 x64 Enterprise machine.
<pre>Public Class Form1

Private Declare Ansi Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal Section As String, ByVal Text As String, ByVal FileName As String) As Int32

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim values(3) As String

values(0) = "NAME1=value1"
values(1) = "NAME2=value2"
values(2) = "NAME3=value3"
values(3) = "NAME4=value4"

MsgBox(WriteIniSection("TEST2", values, "C:\Test.ini").ToString)

End Sub

Public Function WriteIniSection(ByVal Section As String, ByVal Values() As String, ByVal IniPath As String) As Boolean
Dim s As String = ""
For Each a As String In Values
If a <> "" Then s &= a & ControlChars.NullChar
Next
s &= ControlChars.NullChar
Return WritePrivateProfileSection(Section, s, IniPath) = 0
End Function
End Class</pre>

It's not appending at all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900