Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Source] Read/Write to ini (Saving Program Settings)
#1
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.
USB Anti-virus? Try USB Drive Defender
[Image: 8bQCusS.jpg]
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IP Chat Program SOURCE kpn37z 1 1,253 12-08-2012, 07:41 PM
Last Post: Pro-A-Blazed
  [SF Coders] Grabbing an Icon from a File and Saving! [TUT] milopeach 13 3,188 05-15-2012, 06:22 PM
Last Post: OnyxDev
  [Project] Write To Another Process' Memory Block AceInfinity 5 2,410 03-31-2012, 04:05 AM
Last Post: AceInfinity
  [Source ] Hemp Tycoon App [/Source] VB.net KoBE 8 9,355 03-05-2012, 10:30 PM
Last Post: SomeWhiteGuy?
  [Vb.Net]Save user settings with "My.Settings"[TUT] Digital-Punk 7 5,802 11-21-2011, 09:29 AM
Last Post: UrbanByte

Forum Jump:


Users browsing this thread: 1 Guest(s)