Introduction
INI files and the registry are generally things of the past for .NET applications. But what to use? XML seems appropriate, but one look at System.XML
is enough to scare most developers off, especially just to store a few fields. Fortunately, there is a very easy way in .NET to solve this, but one that is usually not seen by developers. In this article, I’ll explain how you can easily store and retrieve your application settings with just a few lines of code.
Background
In Windows 3.1, developers used INI files to store settings. In general, they worked pretty well for simple settings, but were not appropriate for more complex data. INI files also did not account for multiple users, and thus Microsoft invented the Registry.
Along came the Registry with Win32. The registry was fast, hierarchical, multi user, and allowed storage of typed data. But unfortunately, the Registry was a central system component and was not contained as part of the application install.
Next, XML became popular. XML offers fast, hierarchical storage of typed data. However, XML is so flexible that, for most users, doing anything simple is quite an undertaking. Fortunately, there are easier ways than using System.XML
and handling everything yourself.
Using the code
To use the class, there are only two methods you will need to worry about:
void GetSettings(String Key)
Sets the path and filename for the XML settings file.
void WriteSetting(String Key, String KeyValue, bool encode)
Writes a value to the settings file, given a key and value name. The encode
option is to encrypt the value.
TxtServerName.Text = Configure.getSetting("DBServer");
Configure.writeSetting("DBServer", TxtServerName.Text, false);
Dim x As New Configuration.configure
TxtDBServer.Text = CStr(x.getSettingFromVB6("DBServer"))
If the XML file does not exist or if a key/value does not exist, the Write methods will create it and the Get methods will return the default value.
Stage 1: creating the control
This took the longest of the three stages in our case, simply because of the nature of the control and calculating the positions of text, etc. I will not delve into the details of our control, but only the steps that are required for VB6 Interop.
- Create a new Windows Control Library project from within Visual Studio.
- In both the Debug and Release modes of the Property Pages, set the "Register for COM Interop" checkbox.
- Inside the AssemblyInfo.cs file, change the assembly wide attribute
ComVisible
to true
. If it's not already in the configuration file, add it.
[assembly: ComVisible(true)]
That is all that is required to make the project visible to VB6 projects.
Properties
A quick word about these: properties are exposed to VB6 so, like .NET controls, if you want to expose a value, you must wrap it in a Property expression. You cannot just have it visible as a field.
Stage 2: registering the assembly
The library must be registered on the client machine before use by VB6. If it is not registered on the development machine, then it will not be visible in the References dialog of VB6. If it's not registered on the installation machine, then it is a similar problem to if you have not registered a classic DLL or ActiveX control. The "Register for COM Interop" checkbox in VS2005 performs this registration automatically while the environment is running, but un-registers it when VS is closed.
To register the assembly, you must use the .NET equivalent of regsvr32, regasm. This is located in the framework directory, usually "C:\WINDOWS\Microsoft.NET\Frmaework\v2.0.50727". To register it, open a command prompt and run the following command, assuming that the framework directory and the assemblies directory are in the environment's current path.
regasm.exe Assembly.dll
Stage 3: adding to VB6 projects
The secret here is the VBControlExtender
object, which allows a .NET control to be hosted on a VB6 form. However, the first stage is to add a reference to the assembly you just registered. This is accomplished by checking the box in the Project | References menu.
Once that has been accomplished, then the following code can be added to the form's code in the project.
Conclusion
XML files are a widespread standard that allows easy storage of structured typed data. Use of XML files allows easy editing by end users, and even by other software. Using System.XML
, you can easily store your settings in XML files.
History
- March 10, 2008 - Original article.