Reading an INI file in vb 2008
Reading an INI file in vb 2008
Reading an INI file in vb 2008
Reading an INI file in vb 2008
Reading an INI file in vb 2008
Reading an INI file in vb 2008 Reading an INI file in vb 2008 Reading an INI file in vb 2008 Reading an INI file in vb 2008 Reading an INI file in vb 2008 Reading an INI file in vb 2008 Reading an INI file in vb 2008 Reading an INI file in vb 2008
Reading an INI file in vb 2008 Reading an INI file in vb 2008
Reading an INI file in vb 2008
Go Back  Xtreme Visual Basic Talk > > > Reading an INI file in vb 2008


Reply
 
Thread Tools Display Modes
  #1  
Old 02-02-2009, 02:46 PM
michelleqw michelleqw is offline
Junior Contributor
 
Join Date: Jun 2005
Posts: 212
Default Reading an INI file in vb 2008


Dear all,

Since a few days we are working with vb 2008 and we made script for reading a INI file.

Is there a more simple way for reading a INI file? In vb60 we used a dll for reading.

Thanks for any advise.

Nice regards,
Michelle.
Reply With Quote
  #2  
Old 02-03-2009, 09:31 AM
Boxersoft Boxersoft is offline
Newcomer
 
Join Date: Jan 2009
Posts: 3
Default

Below is a class that I use. I don't remember where it came from I'm afraid, so I can't give credit to the original author.

Usage example:
Code:
Dim ini As New IniFile(FileName)
Dim value As String = ini.GetString("SectionName", "KeyName", "Default Value")
Code:
Public Class IniFile
    ' API functions
    Private Declare Ansi Function GetPrivateProfileString _
      Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
      (ByVal lpApplicationName As String, _
      ByVal lpKeyName As String, ByVal lpDefault As String, _
      ByVal lpReturnedString As System.Text.StringBuilder, _
      ByVal nSize As Integer, ByVal lpFileName As String) _
      As Integer
    Private Declare Ansi Function WritePrivateProfileString _
      Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
      (ByVal lpApplicationName As String, _
      ByVal lpKeyName As String, ByVal lpString As String, _
      ByVal lpFileName As String) As Integer
    Private Declare Ansi Function GetPrivateProfileInt _
      Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
      (ByVal lpApplicationName As String, _
      ByVal lpKeyName As String, ByVal nDefault As Integer, _
      ByVal lpFileName As String) As Integer
    Private Declare Ansi Function FlushPrivateProfileString _
      Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
      (ByVal lpApplicationName As Integer, _
      ByVal lpKeyName As Integer, ByVal lpString As Integer, _
      ByVal lpFileName As String) As Integer
    Dim strFilename As String

    ' Constructor, accepting a filename
    Public Sub New(ByVal Filename As String)
        strFilename = Filename
    End Sub

    ' Read-only filename property
    ReadOnly Property FileName() As String
        Get
            Return strFilename
        End Get
    End Property

    Public Function GetString(ByVal Section As String, _
      ByVal Key As String, ByVal [Default] As String) As String
        ' Returns a string from your INI file
        Dim intCharCount As Integer
        Dim objResult As New System.Text.StringBuilder(256)
        intCharCount = GetPrivateProfileString(Section, Key, _
           [Default], objResult, objResult.Capacity, strFilename)
        If intCharCount > 0 Then
            Return Left(objResult.ToString, intCharCount)
        Else
            Return ""
        End If
    End Function

    Public Function GetInteger(ByVal Section As String, _
      ByVal Key As String, ByVal [Default] As Integer) As Integer
        ' Returns an integer from your INI file
        Return GetPrivateProfileInt(Section, Key, _
           [Default], strFilename)
    End Function

    Public Function GetBoolean(ByVal Section As String, _
      ByVal Key As String, ByVal [Default] As Boolean) As Boolean
        ' Returns a boolean from your INI file
        Return (GetPrivateProfileInt(Section, Key, _
           CInt([Default]), strFilename) = 1)
    End Function

    Public Sub WriteString(ByVal Section As String, _
      ByVal Key As String, ByVal Value As String)
        ' Writes a string to your INI file
        WritePrivateProfileString(Section, Key, Value, strFilename)
        Flush()
    End Sub

    Public Sub WriteInteger(ByVal Section As String, _
      ByVal Key As String, ByVal Value As Integer)
        ' Writes an integer to your INI file
        WriteString(Section, Key, CStr(Value))
        Flush()
    End Sub

    Public Sub WriteBoolean(ByVal Section As String, _
      ByVal Key As String, ByVal Value As Boolean)
        ' Writes a boolean to your INI file
        WriteString(Section, Key, CStr(CInt(Value)))
        Flush()
    End Sub

    Private Sub Flush()
        ' Stores all the cached changes to your INI file
        FlushPrivateProfileString(0, 0, 0, strFilename)
    End Sub

End Class
Reply With Quote
  #3  
Old 02-03-2009, 09:55 AM
AtmaWeapon's Avatar
AtmaWeaponReading an INI file in vb 2008 AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Do you need to be using ini files? Microsoft has discouraged their use for nearly a decade now. If you're storing program settings, .NET has an entire Application Settings framework that is as easy to use (if not easier) than ini files or the registry.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 02-03-2009, 12:20 PM
michelleqw michelleqw is offline
Junior Contributor
 
Join Date: Jun 2005
Posts: 212
Default

Thanks for the information.

The idea for using ini files is to keep the code free from any variable that can be change. For example the conection string to a database. The ini file is read by the script and it's not nessacary to rebuilt the project
Reply With Quote
  #5  
Old 02-03-2009, 03:55 PM
Roger_Wgnr's Avatar
Roger_Wgnr Roger_Wgnr is offline
CodeASaurus Hex

Forum Leader
* Expert *
 
Join Date: Jul 2006
Location: San Antonio TX
Posts: 2,427
Default

Application settings saves your configured settings to an XML file that can be changed. This is the new INI file. The settings can also be saved on a Per machine or per user basis. you should look into using the application settings class instead of the INI file.
__________________
Code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. ~Martin Golding
The user is a peripheral that types when you issue a read request. ~Peter Williams
MSDN Visual Basic .NET General FAQ
Reply With Quote
  #6  
Old 02-04-2009, 04:51 AM
michelleqw michelleqw is offline
Junior Contributor
 
Join Date: Jun 2005
Posts: 212
Default

Hello Roger_Wgnr and others,

How simple can we change a xml file for example in the explorer / is it easy to read in vb 2008, in vb6 we where not happy to read a xml. (lot of code)

Nice regards,
Michelle.
Reply With Quote
  #7  
Old 02-04-2009, 05:31 AM
Boxersoft Boxersoft is offline
Newcomer
 
Join Date: Jan 2009
Posts: 3
Default

Michelle, I tend to agree with the others: if the storage mechanism is under your own control, ApplicationSettings is a better approach. I'm currently using .ini files because I need to access data stored that way by another application over which I have no control.

You don't have to worry about reading the raw XML file from VB - Application Settings are all nicely supported by the Framework and the IDE. Try this:
  • In VB2008's Solution Explorer pane, right-click the project and select Properties from the context menu.
  • From the tab to the left, select Settings.
  • Create a few items of various types in the grid.
  • Go to a code window and in e.g. the Form1_Load event handler, type My.Settings.
  • Intellisense should provide you with a list of Settings (all property typed) that you can simply treat as read/write properties.

You can also bind properties of control to Settings so that e.g. the Text properties of TextBox and Label controls etc. can be automatically populated without any user-written code. To see this in action, select a Label or TextBox on a form in VB2008. At the top of the list in the Properties pane, you should see an expandable entry called (ApplicationSettings). Expand that, and you should see that Text is offered (as the property you are most likely to want to bind). The drop-down list next to that should present you with a list of all the string Settings you have created (and even provides you with the facility to add a new one there and then).
Reply With Quote
  #8  
Old 02-04-2009, 09:56 AM
AtmaWeapon's Avatar
AtmaWeaponReading an INI file in vb 2008 AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Quote:
Originally Posted by michelleqw View Post
Hello Roger_Wgnr and others,

How simple can we change a xml file for example in the explorer / is it easy to read in vb 2008, in vb6 we where not happy to read a xml. (lot of code)

Nice regards,
Michelle.
If you are using the application settings class (I linked to documentation on it, please read it as it answers many of your followup questions.) then you aren't even aware that you are reading XML. The settings file loads itself automatically, and all you do is access properties on the class.

Regardless, reading XML in VB .NET is about as easy as any other language. VB6's problem with XML is probably that it is ancient enough that it came before XML, so reading XML wasn't designed into it.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Reading an INI file in vb 2008
Reading an INI file in vb 2008
Reading an INI file in vb 2008 Reading an INI file in vb 2008
Reading an INI file in vb 2008
Reading an INI file in vb 2008
Reading an INI file in vb 2008 Reading an INI file in vb 2008 Reading an INI file in vb 2008 Reading an INI file in vb 2008 Reading an INI file in vb 2008 Reading an INI file in vb 2008 Reading an INI file in vb 2008
Reading an INI file in vb 2008
Reading an INI file in vb 2008
 
Reading an INI file in vb 2008
Reading an INI file in vb 2008
 
-->