Solved

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

Posted on 2006-07-14
2
310 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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