Aside from the handful of Windows API functions (mentioned above) that might serve your INI-handling needs without the need to write your own parsing engine (which is why those functions exist), you do have another option: the Windows Registry. VB6 (at least) provides a few functions for using the registry from your VB programs. These only allow you to play with values in the Software tree of HKCU (and possibly HKLM, though I doubt it), but that's all you really need for what you're describing anyway. Also, what you're describing is the reason VB includes these registry methods in the first place.
First we have the method used to create and save a registry key:
SaveSetting(AppName As String, Section As String, Key As String, Setting As String) I'll try to compare this to a standard INI file.
AppName would be equivalent to the filename of your INI file. For example, let's say I have an application called
MakeItWork.exe, with an INI file called
miw.ini. To migrate from INI to the Windows Registry, I would simply put
miw in place of AppName. Many applications use the value of App.Name instead of a hard-coded value - this is merely personal preference, but can have a few advantages.
Section is just like an INI section:
[FIXES] in
miw.ini would become
fixes as the value of Section. Fairly straightforward.
Key is the value's name, and Setting is the value itself. In
[FIXES] I have a line that reads
fix0024=C:\Windows\explorer.exe, which indicates that the application has fixed explorer.exe, and that this was the 24th fix the application made. The reason for storing an item like this might be so the program can go back through and make sure the fixes it's already made are still in effect, that the files haven't been rebroken since it was last run. However, this is not important. What is important is how I would place this information in the Windows Registry. Here's the completed call:
SaveSetting "miw", "fixes", "fix0024", "C:\Windows\explorer.exe"
The next method is the one used to retrieve information previously stored in the Registry:
GetSetting(AppName As String, Section As String, Key As String, [Default]) As String Note that this one returns a value. All of the arguments here are the same as in
SaveSetting(), except that here Value has been replaced by Default. This is because Value is actually returned by the function. But why the extra arugment then? Well, what if the key you're trying to look up hasn't been saved yet? Default is what the function returns if there isn't a usable Value.
So to get the 24th fix made by MakeItWork, and use the value "xxxx" if no 24th fix was made, I would use the following line:
fix(24) = GetSetting("miw", "fixes", "fix0024", "xxxx")
Pretty simple, no?
Now for the last of the builtin Registry methods:
DeleteSetting(AppName As String, [Section], [Key]) Note that Section and Key are optional; if no Key is provided, the entire Section is removed, and if no Section is provided, everything under AppName is removed. As you likely guessed, if you provide all three, only the specified Key will be removed.
So let us assume that the 24th fix wasn't really a fix at all, since there was nothing to do to begin with. To remove the erroneous entry, I would use the following:
DeleteSetting "miw", "fixes", "fix0024"
All there is to it.
Now granted, using the Registry instead of .INI files means that your settings take a bit more work to propagate to other machines, and if you have no choice about the use of .INI files the above is all moot. However, it is good practice (in the Windows world) to use the Registry instead of .INI files. The Registry is faster, for one.
Well, that's all I have for the moment. Hope this is useful!
- Sendoshin