Click here to Skip to main content
15,881,588 members
Articles / Web Development / ASP.NET
Article

Reading and writing an INI file from ASP.NET 2.0

Rate me:
Please Sign up or sign in to vote.
3.22/5 (5 votes)
15 Sep 2005 67.3K   27   11
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:

VB
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:

VB
' 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:

VB
' 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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
GJ.Coder11-Oct-09 14:47
GJ.Coder11-Oct-09 14:47 
GeneralWin32 API Already contains this functionality Pin
mike-obrien1-Dec-06 11:07
mike-obrien1-Dec-06 11:07 
GeneralRe: Win32 API Already contains this functionality Pin
MooseFruit1-Dec-06 15:25
MooseFruit1-Dec-06 15:25 
GeneralRe: Win32 API Already contains this functionality Pin
cambo198224-Sep-07 22:51
cambo198224-Sep-07 22:51 
GeneralRe: Win32 API Already contains this functionality Pin
12Code26-Mar-08 18:34
12Code26-Mar-08 18:34 
QuestionWhy not use xml? Pin
Anonymous15-Sep-05 13:03
Anonymous15-Sep-05 13:03 
AnswerRe: Why not use xml? Pin
MooseFruit16-Sep-05 7:44
MooseFruit16-Sep-05 7:44 
GeneralRe: Why not use xml? Pin
Anonymous18-Sep-05 0:08
Anonymous18-Sep-05 0:08 
GeneralRe: Why not use xml? Pin
MooseFruit18-Sep-05 15:09
MooseFruit18-Sep-05 15:09 
AnswerRe: Why not use xml? Pin
Edwin Roetman21-Sep-05 0:07
Edwin Roetman21-Sep-05 0:07 
AnswerRe: Why not use xml? Pin
yrleu6-Dec-06 21:29
yrleu6-Dec-06 21:29 

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

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