Support Forums
[Source] Read/Write to ini (Saving Program Settings) - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Visual Basic and the .NET Framework (https://www.supportforums.net/forumdisplay.php?fid=19)
+---- Thread: [Source] Read/Write to ini (Saving Program Settings) (/showthread.php?tid=18723)



[Source] Read/Write to ini (Saving Program Settings) - euverve - 05-12-2011

The following codes shows how to save program settings to an .ini file

[Image: BB06y.png]

Form Codes:
Code:
Imports System.IO

Public Class Form1

    Dim File = Application.StartupPath + "\Test.ini"
    Dim Section = "Settings"
    Dim Author = "Author"
    Dim org = "Organization"

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = ReadIni(File, Section, Author, "")
        TextBox2.Text = ReadIni(File, Section, org, "")
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        writeIni(File, Section, Author, TextBox3.Text)
        writeIni(File, Section, org, TextBox4.Text)
    End Sub
End Class

Module Codes:
Code:
Module modINI
    Private Declare Unicode Function WritePrivateProfileString Lib "kernel32" _
    Alias "WritePrivateProfileStringW" (ByVal lpApplicationName As String, _
    ByVal lpKeyName As String, ByVal lpString As String, _
    ByVal lpFileName As String) As Int32

    Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" _
    Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, _
    ByVal lpKeyName As String, ByVal lpDefault As String, _
    ByVal lpReturnedString As String, ByVal nSize As Int32, _
    ByVal lpFileName As String) As Int32

    Public Sub writeIni(ByVal iniFileName As String, ByVal Section As String, ByVal ParamName As String, ByVal ParamVal As String)
        Dim Result As Integer = WritePrivateProfileString(Section, ParamName, ParamVal, iniFileName)
    End Sub

    Public Function ReadIni(ByVal IniFileName As String, ByVal Section As String, ByVal ParamName As String, ByVal ParamDefault As String) As String
        Dim ParamVal As String = Space$(1024)
        Dim LenParamVal As Long = GetPrivateProfileString(Section, ParamName, ParamDefault, ParamVal, Len(ParamVal), IniFileName)
        ReadIni = Left$(ParamVal, LenParamVal)
    End Function
End Module

Contents of the ini file
Code:
; Comment is in semi colon
[Settings]
Author=euverve/Thatskie84
Organization=Support Forums Inc.



RE: [Source] Read/Write to ini (Saving Program Settings) - AceInfinity - 05-12-2011

This has always been a useful code for some of my applications. a settings.ini or config.ini is useful for applications such as personal notepad apps and such.

Very nice Smile