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
Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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