Hello,
I'm trying to write data to an INI file, I've got some code to read it: http://pastebin.com/Jpwf8XJM.
I've tried to modify this code to get it to be able to write to INI files, but I haven't had much success.
I know there are DLL's designed for this, but I would like to have my application so it can be distributed as a single EXE.
Also, I know there is My.Settings, but I cannot use this, as this INI file is used by another application, that I have not developed.
So, I was wondering if anyone could help me modify this code to write to an INI file, something like

WriteIniValue(IniPath, Section, Item, Value)

Thanks, Matthew.

Recommended Answers

All 3 Replies

Use this:

Imports System.Runtime.InteropServices
Imports System.Text

Namespace Ini
    ''' <summary>
    ''' Create a New INI file to store or load data
    ''' </summary>
    Public Class IniFile
        Public path As String

        <DllImport("kernel32")> _
        Private Shared Function WritePrivateProfileString(section As String, key As String, val As String, filePath As String) As Long
        End Function
        <DllImport("kernel32")> _
        Private Shared Function GetPrivateProfileString(section As String, key As String, def As String, retVal As StringBuilder, size As Integer, filePath As String) As Integer
        End Function

        ''' <summary>
        ''' INIFile Constructor.
        ''' </summary>
        ''' <PARAM name="INIPath"></PARAM>
        Public Sub New(INIPath As String)
            path = INIPath
        End Sub
        ''' <summary>
        ''' Write Data to the INI File
        ''' </summary>
        ''' <PARAM name="Section"></PARAM>
        ''' Section name
        ''' <PARAM name="Key"></PARAM>
        ''' Key Name
        ''' <PARAM name="Value"></PARAM>
        ''' Value Name
        Public Sub IniWriteValue(Section As String, Key As String, Value As String)
            WritePrivateProfileString(Section, Key, Value, Me.path)
        End Sub

        ''' <summary>
        ''' Read Data Value From the Ini File
        ''' </summary>
        ''' <PARAM name="Section"></PARAM>
        ''' <PARAM name="Key"></PARAM>
        ''' <PARAM name="Path"></PARAM>
        ''' <returns></returns>
        Public Function IniReadValue(Section As String, Key As String) As String
            Dim temp As New StringBuilder(255)
            Dim i As Integer = GetPrivateProfileString(Section, Key, "", temp, 255, Me.path)
            Return temp.ToString()

        End Function
    End Class
End Namespace

Ok, Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.