Click here to Skip to main content
Click here to Skip to main content

Reading and writing an INI file from ASP.NET 2.0

, 15 Sep 2005
Rate this:
Please Sign up or sign in to vote.
A class that allows you to quickly read and write keys & values from an INI file in ASP.NET 2.0.

Introduction

I needed a way to quickly & easily store settings in an INI file. There are tons of classes out there, but most seemed to be overkill for what I was trying to do. So I put this class together. It works well for me. I hope someone else will find it valuable.

Here's my example INI, note the [eof] required at the end:

; web application settings

[email]

emailAdminFrom = "user@domain.com"
someOtherSetting = "some value"

[eof]

Here's the class:

Imports Microsoft.VisualBasic
Imports System.IO

Public Class ini
    Public Shared Function read(ByVal iniFile As String, ByVal searchKey As String)

        Dim lenSS As Integer = Len(searchKey)
        Dim locQ As Integer
        Dim newStr As String

        Using sr As StreamReader = _
          New StreamReader(HttpContext.Current.Server.MapPath(iniFile))
            Dim line As String
            ' loop until end of file
            Do
                line = sr.ReadLine()
                ' check each line for a key match
                If Left(line, lenSS) = searchKey Then
                    ' match found, now parse out value
                    ' find the first quote mark
                    locQ = InStr(line, """")
                    ' now create the value
                    newStr = Mid(line, (locQ + 1), ((Len(line) - locQ) - 1))
                    ' return the value
                    Return newStr
                    line = Nothing
                End If
            Loop Until line Is Nothing
            sr.Close()
        End Using
        Return "error"
    End Function

    Public Shared Function write(ByVal iniFile As String, _
           ByVal writeKey As String, ByVal writeValue As String)

        ' temp file #1
        Dim iniTempFile As String = "/tempsettings_del.ini"
        ' temp file #2
        Dim iniTempOrigFile As String = "/tempsettingsorig_del.ini"
        ' length of search string
        Dim lenSS As Integer = Len(writeKey)
        ' eof? t/f
        Dim boolEof As Boolean
        ' string to search for at eof
        Dim strEof As String = "[eof]"
        ' server.mappath to all files used
        Dim iniMappedFile As String = _
           HttpContext.Current.Server.MapPath(iniFile)
        Dim iniMappedTempFile As String = _
           HttpContext.Current.Server.MapPath(iniTempFile)
        Dim iniMappedTempOrigFile As String = _
           HttpContext.Current.Server.MapPath(iniTempOrigFile)

        ' setup file to output to
        Dim sw As StreamWriter = New StreamWriter(iniMappedTempFile)

        ' process overview:
        ' 1. read each line of orig ini file
        ' 2. check for match, if no match, write to temp file
        ' 3. if match, write new value
        ' 4. write rest of orig ini file

        ' new value to write
        Dim strNewValue = writeKey & " = """ & writeValue & """"

        Using sr As StreamReader = New StreamReader(iniMappedFile)
            Dim line As String
            ' loop until end of file
            Do
                line = sr.ReadLine()
                ' first check for eof so we don't write extra blank lines
                If Left(line, 5) = strEof Then boolEof = True

                ' check each line for a key match
                If Left(line, lenSS) = writeKey Then
                    ' match found, write new value to temp file
                    sw.WriteLine(strNewValue)
                Else
                    ' are we at the enf of the file?
                    If boolEof = True Then
                        sw.WriteLine(strEof)
                        ' break out of loop
                        Exit Do
                    End If
                    ' match not found, write line to temp file
                    sw.WriteLine(line)
                End If
            Loop Until line Is Nothing
            sr.Close()
        End Using
        sw.Close()

        ' 1 / 3. move orig file to temp file in case 2nd move fails
        File.Move(iniMappedFile, iniMappedTempOrigFile)

        ' 2 / 3. move temp file to orig file
        File.Move(iniMappedTempFile, iniMappedFile)

        ' 3 / 3. delete renamed, now temp orig file
        File.Delete(iniMappedTempOrigFile)

        ' done
        Return ""
    End Function
End Class

Here's an example usage to retrieve a value:

' path to ini
Dim iniSettings as string = "/settings.ini"

' key to read from
Dim strKey as string = "emailAdminFrom"

' read email address
Dim strEmail = ini.read(iniSettings, strKey)

Here's an example usage to write a value:

' path to ini
Dim iniSettings as string = "/settings.ini"

' key to write to
Dim strKey as string = "emailAdminFrom"

' new value to write
Dim strNewValue as string = "newuseremail@domain.com"

' write email address
ini.write(iniSettings, strKey, strNewValue)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

MooseFruit

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberGJ.Coder11-Oct-09 15:47 
GeneralWin32 API Already contains this functionality Pinmembermike-obrien1-Dec-06 12:07 
GeneralRe: Win32 API Already contains this functionality PinmemberMooseFruit1-Dec-06 16:25 
GeneralRe: Win32 API Already contains this functionality Pinmembercambo198224-Sep-07 23:51 
GeneralRe: Win32 API Already contains this functionality Pinmember8kelvin26-Mar-08 19:34 
QuestionWhy not use xml? PinsussAnonymous15-Sep-05 14:03 
AnswerRe: Why not use xml? PinsussMooseFruit16-Sep-05 8:44 
GeneralRe: Why not use xml? PinsussAnonymous18-Sep-05 1:08 
GeneralRe: Why not use xml? PinmemberMooseFruit18-Sep-05 16:09 
AnswerRe: Why not use xml? PinmemberEdwin Roetman21-Sep-05 1:07 
AnswerRe: Why not use xml? Pinmemberyrleu6-Dec-06 22:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411028.1 | Last Updated 15 Sep 2005
Article Copyright 2005 by MooseFruit
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid