Introduction
In this article, I tried to implement the following things:
- Manipulate application configuration data and settings as Name Value collection
- Provide advanced
GetSetting
and SaveSetting
functions - Provide advanced INI file handling
- Implement escape sequence characters for settings
Background
VB6 provides GetSetting
and SaveSetting
functions to programmers to manipulate application settings. These settings are stored in HKEY_CURRENT_USER \ Software \ VB and VBA Program Settings\ location of the windows registry.
Disadvantages
- Note that these settings store under current user settings of registry. So in multi user Windows system, each user may have different settings.
- Since the speed of Windows depends up on the Windows registry size also, we can't store a large amount of information on the registry.
- Moving configuration data from one machine to another machine is very hard.
Please note that the earlier version of Windows provides 2 functions namely
getprivateprofilestring
, writeprivateprofilestring
for manipulation of application data by using files. But Microsoft states that "This function is provided only for compatibility with 16-bit Windows-based applications. Win32-based applications should store initialization information in the registry."
So I implemented Name Value collection in VB6 to read and write settings of an application from a text file so that we can easily move the application from one machine to another machine.
Using the Code
Settings.FileName = "ConverterSettings.ini"
Settings.LoadContents
Settings.SaveCollectionToFile
CollectionName("Name/Key") = value
CollectionName.Remove(index / Key)
Public Function GetMySetting(AppName As String, Section As String, _
Key As String, Optional Default As String) As String
GetMySetting = Settings.GetItem(AppName & "_" & Section & "_" & Key,_
, Default)
End Function
Public Function SaveMySetting(AppName As String, Section As String, _
Key As String, Setting As String)
Settings(AppName & "_" & Section & "_" & Key) = Setting
End Function
List of functions implemented on class module and their meaning:
Name | Usage | Scope | Type |
ProcessEscapeSequence | Converts escape sequence characters | Private | Function |
ProcessINIContents | Parses file content to string lines | Private | Subroutine |
ProcessLine | Load Keys and values from file lines | Private | Subroutine |
Add | Adds new item to collection | Public | Function |
Count | Returns number of items in collection | Public | Property Get |
GetItem | Reads an item safely from collection | Public | Function |
LoadContents | Loads collection from file | Public | Function |
Remove | Removes an item from collection | Public | Subroutine |
SaveCollectionToFile | Saves contents of collection to a file | Public | Function |
Points of Interest
I added a module that processes file and Windows operations with proper status codes with this article.
Name | Usage | Scope | Type |
ExplorerOpen | Open a file in explorer | Public | Subroutine |
ExplorerShowFile | Locate a file in explorer | Public | Subroutine |
FileAppend | Append information to file | Public | Function |
FileAppendString | Append string to file | Public | Function |
FileExists | Check file exists in particular location or not | Public | Function |
FileKillSafe | Kill a file from disk | Public | Function |
FileMakeString | Replace file contents with a string | Public | Function |
FileRead | Read file contents as bytearray | Public | Function |
FileReadString | Read contents of a file in string format | Public | Function |
FileStore | Store binary information(byte array) to a file | Public | Function |
FillCombo | Fill a combo from file content string | Public | Subroutine |
FolderExists | Check folder exists in given path or not | Public | Function |
FolderKill | Delete this directory and all the SubFolders it contains. | Public | Function |
GetMySetting | Replacement of GetSetting function | Public | Function |
PathFileName | Get file name part of full file path | Public | Function |
PathParent | Get Parent folder name | Public | Function |
PathRemoveSlash | Remove slash from right most end | Public | Function |
ProcessExecuteandWait | Execute a file and wait for it to finish | Public | Function |
SaveMySetting | Replacement of SaveSetting function | Public | Function |
StringStripNull | Return a string without the chr$(0) terminator. | Public | Function |
WindowsTemporaryFolder | Get windows temporary folder path | Public | Function |
History
- Version 1 released on April 20, 2008