Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
INI Files
Message
General information
Forum:
ASP.NET
Category:
Other
Title:
Miscellaneous
Thread ID:
00796218
Message ID:
00796247
Views:
13
This message has been marked as the solution to the initial question of the thread.
Hi Wilson,

Here is a class that will read/write to an INI file:
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Text


Namespace Ini
   '/ <summary>
   '/ Create a New INI file to store or load data
   '/ </summary>
   
   Public Class IniFile
      Public path As String
      
      
        <DllImport("kernel32")> _
        Private Shared Function GetPrivateProfileString(ByVal section As String, ByVal key As String, ByVal def As String, ByVal retVal As StringBuilder, ByVal size As Integer, ByVal filePath As String) As Integer
        End Function

        <DllImport("kernel32")> _
        Private Shared Function WritePrivateProfileString(ByVal section As String, ByVal key As String, ByVal val As String, ByVal filePath As String) As Long
        End Function


        '/ <summary>
        '/ INIFile Constructor.
        '/ </summary>
        '/ <param name="INIPath"></param>
        Public Sub New(ByVal INIPath As String)
            path = INIPath
        End Sub 'New

        '/ <summary>
        '/ Write Data to the INI File
        '/ </summary>
        '/ <param name="Section"></param>
        '/ Section name
        '/ <param name="Key"></param>
        '/ Key Name
        '/ <param name="Value"></param>
        '/ Value Name
        Public Sub IniWriteValue(ByVal Section As String, ByVal Key As String, ByVal Value As String)
            WritePrivateProfileString(Section, Key, Value, Me.path)
        End Sub 'IniWriteValue


        '/ <summary>
        '/ Read Data Value From the Ini File
        '/ </summary>
        '/ <param name="Section"></param>
        '/ <param name="Key"></param>
        '/ <param name="Path"></param>
        '/ <returns></returns>
        Public Function IniReadValue(ByVal Section As String, ByVal Key As String) As String
            Dim temp As New StringBuilder(255)
            Dim i As Integer = GetPrivateProfileString(Section, Key, "", temp, 255, Me.path)
            Return temp.ToString()
        End Function 'IniReadValue 
    End Class 'IniFile
End Namespace 'Ini
To call the class:
' To read the INI file
Dim ini As New IniFile("C:\test.ini")
fname.Text = ini.IniReadValue("Info", "FirstName")
lname.Text = ini.IniReadValue("Info", "LastName")

' To write to the INI file
Dim ini As New IniFile("C:\test.ini")
ini.IniWriteValue("Info", "FirstName", fname.Text)
ini.IniWriteValue("Info", "LastName", lname.Text)
>How do i read INI files in VB .NET
>
>i tried using the APIs for INI files but i couldn't make them work.
>
>does anyone knows how to?
>
>ho do i convert this to VB .NET
>
>str = string(255,0)
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform