Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

How to read 2 string variables from INI file in VB.NET

I want to read two key values from an INI file that will be different on all computers which is read by my VB.Net application that will reside on all machiness.

How do I parse the INI file to obtain the values within...

Example
[Info]
ID=434
Name=Test
0
Sc0ttRB
Asked:
Sc0ttRB
1 Solution
 
mastooCommented:
Use an api call.  Declare it:

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

And wrap it in a .Net friendly function:

Public Function ReadStringFromIniFile(ByVal sSection As String, ByVal sKey As String, _
   ByVal sDefault As String, ByVal sIniFile As String) As String

    Dim sString As String
      Dim sBuffer As New String(" "c, 255)
      Dim iLen As Integer
      iLen = GetPrivateProfileString(sSection, sKey, sDefault, sBuffer, 254, sIniFile)
      sString = Left(sBuffer, iLen)
      Return sString
  End Function
0
 
bman9111Commented:
what value are u wanting to capture in the variable...

I am assuming 434 and test.


this is what I would do since u are using .net

Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim file As IO.StreamReader
        Dim line as string
        Dim variable1, variable2 As String
        file = New System.IO.StreamReader(Application.StartupPath & "\test.txt")
       
        While file.Peek <> -1
            line = file.ReadLine()
            If line.StartsWith("ID") Then
                line = line.Substring(3)
                variable1 = line

            ElseIf line.StartsWith("Name") Then
                line = line.Substring(5)
                variable2 = line
            End If

        End While

        file.Close()
        MessageBox.Show(variable1 & vbCrLf & variable2)

    End Sub
End Class
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now