Gaza Gaza - 4 months ago 81x Question

Read INI in Visual Basic vb.NET

I'm trying to read a INI file then dump the values into the registry for a setup application to function.

The INI file looks like this

[Setup Components]

Sybase Adaptive Server Anywhere Client=Yes

Borland Database Engine=Yes

BDERoot=c:\Program Files\Borland\BDE

Program Shortcuts=Yes

ODBC Data Service Name=Yes

Local Client = Yes

Sybase Admin = Yes

Sybase Directory= C:\Program Files\SQL Anywhere 16


Word Link=Yes

Installation Root Folder=c:\program


[Program Shortcuts]

Start Menu=Yes

Program Title=PROGRAM

EXE Pathname=c:\program.exe


Starting Directory=c:\


[ODBC Data Service Name]

Data Service Name=DBNAME

Database File Name=c:\database\database.db

Database Name=DBNAME

Server Name=ENGINE

Comm Links=TCPIP{}






Display Name=SQL Anywhere - DBNAME
Service Group=SQLANYServer
Params=-n DBNAME -x TCPIP{} "C:\database\database.db" -n DBNAME

So I need all those items to be dumped into the registry easily.

I'm using Visual Studio 2015, I did attempt to use Ludvik Jerabek's INI reader ( but had no luck getting that to function!

The code i did use for the above was the following:

Dim ini = New IniFile()
Dim readValue = ini.Sections("Service").Keys("Service Name")

When running this code i got the following error : "Conversion from string "Service" to type "Integer" is not valid. -Also this method means naming each and every key in the INI file which would be quite some task!

I then went down another method after reading some help questions on here and i used the following:

Private Declare Auto Function GetPrivateProfileString Lib "kernel32" (ByVal lpAppName As String,
ByVal lpKeyName As String,
ByVal lpDefault As String,
ByVal lpReturnedString As StringBuilder,
ByVal nSize As Integer,
ByVal lpFileName As String) As Integer

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim sb As StringBuilder
sb = New StringBuilder(500)
Dim readVal = GetPrivateProfileString("Service", "Service Name", "", sb, sb.Capacity, "setup.ini")
End Sub

However this just returns "0"

Any help would be grateful to find a way to get this reading from the INI and dumping to the registry


Using the given IniFile class, this will get you to the config setting values:

Private Sub INIButtonTest_Click(sender As Object, e As EventArgs) Handles INIButtonTest.Click

        Dim iniFilePath As String = "H:\Dev\StackOverflow\StackOverflowTest\StackOverflowApp\bin\Debug\test.ini"

        Dim myIniFile As New IniFile

        Dim myValue As String = getIniValue(myIniFile, "Service", "Service Name")

        If Not String.IsNullOrEmpty(myValue) Then
            MessageBox.Show(String.Format("Found value: [{0}]", myValue))
        End If

    Catch ex As Exception
        MessageBox.Show(String.Concat("Something went wrong:", ex.Message))
    End Try
End Sub

Private Function getIniValue(iniFileInstance As IniFile, sectionName As String, sectionKey As String) As String

    Dim myValue As String = String.Empty

    For Each sect As IniFile.IniSection In iniFileInstance.Sections
        If sect.Name = sectionName Then
            For Each key As IniFile.IniSection.IniKey In sect.Keys
                If key.Name = sectionKey Then
                    myValue = key.GetValue
                    Exit For
                End If
            Exit For
        End If

    Return myValue

End Function