Solved

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now