Results 1 to 10 of 10

Thread: Question on INI files

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    My program uses INI files to store multitudes of information, such as user profile, etc. I need to know why when I read an INI file (getprivateprofile...) sometimes the returned string is different from the actuall content. Right now I'm using notepad to write the ini so I can test several possible scenarios, but the Win API only returns the current correct content if I modify my program in some way.

    In other words, my refresh function only returns the current content of the INI from when it was first read, and sticks with that. Even if the file changes while the program is running.

    Thanks in advance for any help I recieve.

    -agent

    BTW, For the purposes of my program, I cannot use the system registry to store the info. I need the user to be able to edit the file by hand or replace it if neccesary.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    Why not just parse it yourself as a text file?

    You know that each section starts with a [SectionTitle] and that each attribute is a Variable=Value string.

    So just write a small routine that parses through it :

    Code:
    Dim sSection as String
    Dim sVariable as String
    Dim sValue as String
    Dim sLine as String
    
    Open "ini file" For Input As #1
    Line Input #1, sLine
    Do While Not eof(1)
      If InStr(sLine,"[") = 1 Then ' MUST start the line
        sSection = Mid(sLine,2,InStr(sLine,"]"))
      Else
        If InStr(sLine,"=") > 0 Then
          sVariable = Mid(sLine,1,InStr(sLine,"=")-1)
          sValue = Mid(sLine,InStr(sLine,"=")+1)
        
          Call DoWhatever(sSection, sVariable, sValue)
        End if
      End If
      Line Input #1, sLine
    Loop
    Close(1)
    This can be easily modified if you want to parse it specifically for a particular section and a particular field

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    Unfortunatly, my program cannot easily be rewritten to use that code by the deadline that was imposed. I have it set up so that a certain function returns all of the sections, another returns all of the entries in a section. There's got to be a way to tell windows that I need the most recent version of the ini file.

    For me to manually parse the INI I'd need to make vb equivlants of the api code. This includes the not-so-common EnumerateSections and EnumerateEntries. Plus I'd need a method of removing sections and entries from the INI.

    Thanks for your help anyway

    Anybody els up for this one?

  4. #4
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    I think you are describing a 'feature' of Windows called Lazy Writing - when you save the .INI file Windows says, 'ok, it's saved', but it doesn't actually change what's on your hard disk until it has a free moment to do it - any other programs wanting to know what data is in the file should be told the right information by the operating system but this doesn't seem to always be the case.
    Here is an experiment for you to try - stop all programs running on your machine - start editing an .INI file. Save the file, and listen to the harddisk. Normally you get very little (if anything at all) noise from the harddisk at the point you save the file, then a second or so later you hear a quick 'rattle' which is the data actually being saved.

    One thing I've done to resolve this problem is to put a DoEvents command before reading an .INI file for the first time - this way the processer gets a chance to 'catch up' with pending events (of which one should be to save the .INI file).

    It's a pain, I know.

    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    The DoEvents didn't work. But I noticed that if I wait till after the harddrive activity, the API returns the correct information. Is there a way to make windows update the "Lazy Writing" mechanism?

  6. #6
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    Nope... its an "undocumented feature" of the operating system.

    What you "could" do is get the timestamp of the file BEFORE you wrote to it (GetFileAttr I think) and then go into an infinate loop waiting for it to change before you bother reading the information.

    That way you are guaranteed of getting the new one as the system MUST set the Last Updated timestamp when its done.... Providing of course its "lazy" and it writes that BEFORE it writes to the file

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    Ok, you know where I can find vb equivilants of all the INI related winapi calls? (Manual parsing I mean)

  8. #8
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    You have the code I wrote above... all it would take is the "tinest" bit of lateral thinking to modify it to resemble all the functions you need.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    I took a look at your code above and it gave me an idea. Maybe I should load the entire INI into an array and modify it in memory. Then, when I want to write it, just write the array to disk. Writing it into an array makes it easy to call up a particular line of text or recall a particular line without having to reload the file.

    I'm writing it to an ActiveX DLL and will freely distribute the dll and source when I'm done.

    Again, thanks for all the help here.

  10. #10
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    I'm not sure about this so be careful - but if you look in CONTROL PANEL / SYSTEM / PERFORMANCE / FILE SYSTEM / TROUBLESHOOTING you can disable Write-Behind Caching - I think this is the techno-speak for 'lazy writing'. I can't 100% confirm this is the case but if it is you will notice the PC runs slower but you won't have any more problems with .INI files not saving. Check with a Windows guru first...

    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width