Solved

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

Posted on 2006-07-14
2
311 Views
Last Modified: 2012-08-13
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
Comment
Question by:Sc0ttRB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 21

Expert Comment

by:mastoo
ID: 17111687
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
 
LVL 8

Accepted Solution

by:
bman9111 earned 500 total points
ID: 17113298
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question