Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Reading and writing an INI file from ASP.NET 2.0

0.00/5 (No votes)
15 Sep 2005 1  
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