Using INI files to save changes

I'm planning to use a .ini file to save changes on my program like the pictures used for my MDIform picture so that the picture stays permanently in there when i load the program again, and disabling some forms from startup(Kinda like them tip of the day forms in some office applications that you can disable so that they don't run at all when you run the program again.) so far, i don't know how to code my program to use such features or even connecting to an ini file at all. Please help..

i'm using Visual Basic 6 by the way..

thank you

Comments

  • [b][red]This message was edited by the walrus at 2005-2-5 17:23:7[/red][/b][hr]
    if you want it in the form of an ini file you'll have to make an ini file parser. here's a very simple parser that could probably do what you want, but it's not a very advanced parser and may mess up if it's edited manually, like many people do with ini files (ie: it's very sensitive to white space; unlike the common ini file. it wouldnt be hard to make a better parser though. this is just very basic)

    [code]
    Public Const INIFile = "c:myini.ini"
    Public Variable1, Variable2, Variable3

    Public Sub SaveToINI()

    Dim ff As Long, INIString As String

    ff = FreeFile
    Open INIFile For Binary As #ff
    INIString = "Variable1=" & Variable1 & vbCrLf
    Put #ff, , INIString
    INIString = "Variable2=" & Variable2 & vbCrLf
    Put #ff, , INIString
    INIString = "Variable3=" & Variable3 & vbCrLf
    Put #ff, , INIString
    Close #ff

    End Sub

    Public Sub LoadFromINI()

    Dim ff As Long, fStr As String, fArr() As String, i As Long
    Dim xVariable As String, xValue As String

    ff = FreeFile
    Open INIFile For Binary As #ff
    fStr = String(LOF(ff), Chr(0))
    Get #ff, , fStr
    Close #ff
    fArr = Split(fStr, vbCrLf)
    For i = 0 To UBound(fArr)
    If InStr(fArr(i), "=") Then
    xVariable = Left(fArr(i), InStr(fArr(i), "=") - 1)
    xValue = Right(fArr(i), Len(fArr(i)) - InStr(fArr(i), "="))
    Select Case LCase(xVariable)
    Case "variable1":
    Variable1 = xValue
    Case "variable2":
    Variable2 = xValue
    Case "variable3":
    Variable3 = xValue
    End Select
    End If
    Next

    End Sub
    [/code]

    to use it, just put that code in a module and rename Variable1, Variable2, and Variable3 to the variable's you want to save and load the values from (and also add any variables if you want to store more than 3). and also change INIFile's value to save to where you want it to (right now it's set to save to c:myini.ini).


  • : I'm planning to use a .ini file to save changes on my program like the pictures used for my MDIform picture so that the picture stays permanently in there when i load the program again, and disabling some forms from startup(Kinda like them tip of the day forms in some office applications that you can disable so that they don't run at all when you run the program again.) so far, i don't know how to code my program to use such features or even connecting to an ini file at all. Please help..
    :
    : i'm using Visual Basic 6 by the way..
    :
    : thank you
    :

    Why not use the windows registry? VB6 includes the GetSetting/SaveSetting keywords that make storing and retrieving custom registry values very easy. You don't need to worry about reading and parsing INI files, just read the key values.

  • : [b][red]This message was edited by the walrus at 2005-2-5 17:23:7[/red][/b][hr]
    : if you want it in the form of an ini file you'll have to make an ini file parser. here's a very simple parser that could probably do what you want, but it's not a very advanced parser and may mess up if it's edited manually, like many people do with ini files (ie: it's very sensitive to white space; unlike the common ini file. it wouldnt be hard to make a better parser though. this is just very basic)
    :
    : [code]
    : Public Const INIFile = "c:myini.ini"
    : Public Variable1, Variable2, Variable3
    :
    : Public Sub SaveToINI()
    :
    : Dim ff As Long, INIString As String
    :
    : ff = FreeFile
    : Open INIFile For Binary As #ff
    : INIString = "Variable1=" & Variable1 & vbCrLf
    : Put #ff, , INIString
    : INIString = "Variable2=" & Variable2 & vbCrLf
    : Put #ff, , INIString
    : INIString = "Variable3=" & Variable3 & vbCrLf
    : Put #ff, , INIString
    : Close #ff
    :
    : End Sub
    :
    : Public Sub LoadFromINI()
    :
    : Dim ff As Long, fStr As String, fArr() As String, i As Long
    : Dim xVariable As String, xValue As String
    :
    : ff = FreeFile
    : Open INIFile For Binary As #ff
    : fStr = String(LOF(ff), Chr(0))
    : Get #ff, , fStr
    : Close #ff
    : fArr = Split(fStr, vbCrLf)
    : For i = 0 To UBound(fArr)
    : If InStr(fArr(i), "=") Then
    : xVariable = Left(fArr(i), InStr(fArr(i), "=") - 1)
    : xValue = Right(fArr(i), Len(fArr(i)) - InStr(fArr(i), "="))
    : Select Case LCase(xVariable)
    : Case "variable1":
    : Variable1 = xValue
    : Case "variable2":
    : Variable2 = xValue
    : Case "variable3":
    : Variable3 = xValue
    : End Select
    : End If
    : Next
    :
    : End Sub
    : [/code]
    :
    : to use it, just put that code in a module and rename Variable1, Variable2, and Variable3 to the variable's you want to save and load the values from (and also add any variables if you want to store more than 3). and also change INIFile's value to save to where you want it to (right now it's set to save to c:myini.ini).
    :
    :
    :

    The Walrus:

    thanks for the help, i'm sorry if i just reply to you this late, i've recently tried this code out. Anyway, it works..It save my wallpaper location and now my wallpaper settings are permanent XD. However i did experience this:
    i'll show you

    Public Const IniFile = "i:phillyControl CentreCCsetup.ini"
    Public CCDBdata 'my versions from your example
    Public felisDBdata
    Public remisDBdata

    Public Sub SaveDBdatatoINI()
    Dim dd As Long
    Dim INIDBstring As String

    dd = FreeFile
    Open IniFile For Binary As #dd
    INIDBstring = "[Databases] " & vbCrLf & vbCrLf & "CCDBdata=" & CCDBdata & vbCrLf
    Put #dd, , INIDBstring
    INIDBstring = "" & vbCrLf & "felisDBdata=" & felisDBdata & vbCrLf
    Put #dd, , INIDBstring
    INIDBstring = "" & vbCrLf & "remisDBdata=" & remisDBdata & vbCrLf
    Put #dd, , INIDBstring
    Close #dd
    End Sub

    Public Sub LoadDBdatafromINI()
    Dim dd As Long
    Dim dStr As String
    Dim dArr() As String
    Dim ii As Long
    '
    Dim DBvariable As String
    Dim DBvalue As String

    dd = FreeFile
    Open IniFile For Binary As #dd
    dStr = String(LOF(dd), Chr(0))
    Get #dd, , dStr
    Close #dd
    dArr = Split(dStr, vbCrLf)
    For ii = 0 To UBound(dArr)
    If InStr(dArr(i), "=") Then
    DBvariable = Left(dArr(ii), InStr(dArr(ii), "=") - 1)
    DBvalue = Right(dArr(ii), Len(dArr(ii)) - InStr(dArr(ii), "="))

    Select Case LCase(DBvariable)
    Case "CCDBdata":
    CCDBdata = DBvalue
    Case "felisDBdata":
    felisDBdata = DBvalue
    Case "remisDBdata":
    remisDBdata = DBvalue
    End Select
    End If
    Next
    CCcon.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & DBvalue
    End Sub

    and this:

    Private Sub CmdSave_Click()
    MsgBox "Saved settings"
    CDDBdata = Text1.Text
    felisDBdata = Text2.Text
    remisDBdata = Text3.Text
    Call SaveDBdatatoINI

    I'm using that to get CCDBdata, felisDBdata and remisDBdata's paths however, after it call for the SaveDBdatatoINI, which is similar from the SavetoINI example you showed me, i check my INI file and there was blank the paths weren't placed in the INI file, but the "ccDBdata=",etc. lines were..

    i wonder why??

    so far, i'm trying to save the path of my database using the INI file to make the program portable in some sense..



  • : : [b][red]This message was edited by the walrus at 2005-2-5 17:23:7[/red][/b][hr]
    : : if you want it in the form of an ini file you'll have to make an ini file parser. here's a very simple parser that could probably do what you want, but it's not a very advanced parser and may mess up if it's edited manually, like many people do with ini files (ie: it's very sensitive to white space; unlike the common ini file. it wouldnt be hard to make a better parser though. this is just very basic)
    : :
    : : [code]
    : : Public Const INIFile = "c:myini.ini"
    : : Public Variable1, Variable2, Variable3
    : :
    : : Public Sub SaveToINI()
    : :
    : : Dim ff As Long, INIString As String
    : :
    : : ff = FreeFile
    : : Open INIFile For Binary As #ff
    : : INIString = "Variable1=" & Variable1 & vbCrLf
    : : Put #ff, , INIString
    : : INIString = "Variable2=" & Variable2 & vbCrLf
    : : Put #ff, , INIString
    : : INIString = "Variable3=" & Variable3 & vbCrLf
    : : Put #ff, , INIString
    : : Close #ff
    : :
    : : End Sub
    : :
    : : Public Sub LoadFromINI()
    : :
    : : Dim ff As Long, fStr As String, fArr() As String, i As Long
    : : Dim xVariable As String, xValue As String
    : :
    : : ff = FreeFile
    : : Open INIFile For Binary As #ff
    : : fStr = String(LOF(ff), Chr(0))
    : : Get #ff, , fStr
    : : Close #ff
    : : fArr = Split(fStr, vbCrLf)
    : : For i = 0 To UBound(fArr)
    : : If InStr(fArr(i), "=") Then
    : : xVariable = Left(fArr(i), InStr(fArr(i), "=") - 1)
    : : xValue = Right(fArr(i), Len(fArr(i)) - InStr(fArr(i), "="))
    : : Select Case LCase(xVariable)
    : : Case "variable1":
    : : Variable1 = xValue
    : : Case "variable2":
    : : Variable2 = xValue
    : : Case "variable3":
    : : Variable3 = xValue
    : : End Select
    : : End If
    : : Next
    : :
    : : End Sub
    : : [/code]
    : :
    : : to use it, just put that code in a module and rename Variable1, Variable2, and Variable3 to the variable's you want to save and load the values from (and also add any variables if you want to store more than 3). and also change INIFile's value to save to where you want it to (right now it's set to save to c:myini.ini).
    : :
    : :
    : :
    :
    : The Walrus:
    :
    : thanks for the help, i'm sorry if i just reply to you this late, i've recently tried this code out. Anyway, it works..It save my wallpaper location and now my wallpaper settings are permanent XD. However i did experience this:
    : i'll show you
    :
    : Public Const IniFile = "i:phillyControl CentreCCsetup.ini"
    : Public CCDBdata 'my versions from your example
    : Public felisDBdata
    : Public remisDBdata
    :
    : Public Sub SaveDBdatatoINI()
    : Dim dd As Long
    : Dim INIDBstring As String
    :
    : dd = FreeFile
    : Open IniFile For Binary As #dd
    : INIDBstring = "[Databases] " & vbCrLf & vbCrLf & "CCDBdata=" & CCDBdata & vbCrLf
    : Put #dd, , INIDBstring
    : INIDBstring = "" & vbCrLf & "felisDBdata=" & felisDBdata & vbCrLf
    : Put #dd, , INIDBstring
    : INIDBstring = "" & vbCrLf & "remisDBdata=" & remisDBdata & vbCrLf
    : Put #dd, , INIDBstring
    : Close #dd
    : End Sub
    :
    : Public Sub LoadDBdatafromINI()
    : Dim dd As Long
    : Dim dStr As String
    : Dim dArr() As String
    : Dim ii As Long
    : '
    : Dim DBvariable As String
    : Dim DBvalue As String
    :
    : dd = FreeFile
    : Open IniFile For Binary As #dd
    : dStr = String(LOF(dd), Chr(0))
    : Get #dd, , dStr
    : Close #dd
    : dArr = Split(dStr, vbCrLf)
    : For ii = 0 To UBound(dArr)
    : If InStr(dArr(i), "=") Then
    : DBvariable = Left(dArr(ii), InStr(dArr(ii), "=") - 1)
    : DBvalue = Right(dArr(ii), Len(dArr(ii)) - InStr(dArr(ii), "="))
    :
    : Select Case LCase(DBvariable)
    : Case "CCDBdata":
    : CCDBdata = DBvalue
    : Case "felisDBdata":
    : felisDBdata = DBvalue
    : Case "remisDBdata":
    : remisDBdata = DBvalue
    : End Select
    : End If
    : Next
    : CCcon.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & DBvalue
    : End Sub
    :
    : and this:
    :
    : Private Sub CmdSave_Click()
    : MsgBox "Saved settings"
    : CDDBdata = Text1.Text
    : felisDBdata = Text2.Text
    : remisDBdata = Text3.Text
    : Call SaveDBdatatoINI
    :
    : I'm using that to get CCDBdata, felisDBdata and remisDBdata's paths however, after it call for the SaveDBdatatoINI, which is similar from the SavetoINI example you showed me, i check my INI file and there was blank the paths weren't placed in the INI file, but the "ccDBdata=",etc. lines were..
    :
    : i wonder why??
    :
    : so far, i'm trying to save the path of my database using the INI file to make the program portable in some sense..
    :
    :
    :
    :

    Try setting a breakpoint in this line:
    [code]INIDBstring = "" & vbCrLf & "felisDBdata=" & felisDBdata & vbCrLf[/code]
    and check whether "felisDBdata" contains any valid value. The code looks fine to me, besides variables are declared as Variants (which I don't like and can't be ported to VB.NET either; from a technical standpoint, however, they are fine too).

    I can point out a couple of issues and ideas, though - and my opinion.

    I don't really like binary access for text files though.
    VB can let you Open files for either Input or Output, creating plain text files - not that they are not binary files anyway - and has functions used to handle these, namely Line Input and Split() or Instr.
    The idea would be of reading an INI file line(with "Line Input" or "Input Line", now I don't remember :/ ), then Split() the value for the key. Or find the "=" sign with Instr, and then use Left(), Mid(), or Right().

    Also, check out the file with an hex editor: some characters are interpreted by text editing programs as "end of file", so while being perfectly valid binary values for a binary file, they aren't for a text file (and that's why I don't like binary access for text files).

    Also, you can (and I'd advise to... some things are forever) create a CIniFile class, which holds a Collection of CIniKeyValue objects and can read, parse, and save .ini files. I'll leave the implementation of that, if you feel like handling it that way, to you (it's not that hard).
  • : Also, you can (and I'd advise to... some things are forever) create a CIniFile class, which holds a Collection of CIniKeyValue objects and can read, parse, and save .ini files. I'll leave the implementation of that, if you feel like handling it that way, to you (it's not that hard).
    :

    A CIniKeyValue and CInifile? I don't get it? Is this part of VB or do i have to declare this myself?

  • : : Also, you can (and I'd advise to... some things are forever) create a CIniFile class, which holds a Collection of CIniKeyValue objects and can read, parse, and save .ini files. I'll leave the implementation of that, if you feel like handling it that way, to you (it's not that hard).
    : :
    :
    : A CIniKeyValue and CInifile? I don't get it? Is this part of VB or do i have to declare this myself?
    :

    Write them yourself. It's a good excercice, and two well-written classes will last in time. ;)
    In the end, "reusability" is one of the reason why people use classes.
    So, I was suggesting that invest some time now, jolt down a couple classes, then in every project you need them, you just throw them in.
    No coding, no debugging. Definitely pays back the time, in my opinion; but still, these are my two cents.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories