Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Advanced Collection for VB6 Application Settings

3.00/5 (2 votes)
29 Apr 2008GPL33 min read 1   1.5K  
Advanced Collection (like ASP.NET name/value collection) for VB6 Application settings

Introduction

In this article, I tried to implement the following things:

  1. Manipulate application configuration data and settings as Name Value collection
  2. Provide advanced GetSetting and SaveSetting functions
  3. Provide advanced INI file handling
  4. 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

  1. Note that these settings store under current user settings of registry. So in multi user Windows system, each user may have different settings.
  2. 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.
  3. 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

VB.NET
'Assigns filename to collection to perform operation
Settings.FileName = "ConverterSettings.ini"

'Load collection items from file 
Settings.LoadContents

'Save collection to file
Settings.SaveCollectionToFile

'Add / Modify an item
CollectionName("Name/Key") = value

'Remove an item
CollectionName.Remove(index / Key)

'Replacement of GetSetting function
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

'Replacement of SaveSetting 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

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)