Click here to Skip to main content
16,011,883 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
How can I delete an ini file's section and its data in VB.NET?
Actually, I can delete only data but not the title of the section:

VB
Private Declare Ansi Function WritePrivateProfileString _
Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpString As String, _
ByVal lpFileName As String) As Integer
Posted
Updated 28-Jul-15 0:42am
v2

algoram you can use my library for INI file's processing in C# or VB.NET[^], it can enable you to work with file's sections and their keys just like with any other collection in .NET, for example like this:
VB
Dim ini As New IniFile()
ini.Load("path to a .ini file")

' Remove section, with a specified name, together with its keys.
ini.Sections.Remove("section name")
' Remove section, on specified location, together with its keys.
ini.Sections.RemoveAt(0)
 
Share this answer
 
You can use the same function like this to delete the section:
VB
Private Declare Ansi Function DeletePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileStringA" ( _
    ByVal Section As String, _
    ByVal NoKey As Integer, _
    ByVal NoSetting As Integer, _
    ByVal FileName As String) _
As Integer

Public Sub EraseINISection(ByVal INIFile As String, ByVal Section As String)
    DeletePrivateProfileSection(Section, 0, 0, INIFile)
End Sub
 
Share this answer
 
Comments
algoram 28-Jul-15 6:53am    
i tried this but i have an exception
To be honest, you shouldn't be using INI files any more - there are much more modern (and more friendly!) ways to deal with storing such data.

Windows doesn't provide any method for deleting INI file sections (or indeed, any other information) so your only solution would be to copy the whole file up to the section, skip it, and copy the rest into a new file. Rename the original, then rename the new file.

Seriously: look at something a bit more modern!
 
Share this answer
 
Comments
Leo Chapiro 28-Jul-15 6:46am    
The section should be deleted by calling DeletePrivateProfileSection(Section, 0, 0, INIFile)

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