Click here to Skip to main content
15,565,020 members
Articles / Desktop Programming / Win32
Posted 11 Mar 2008


27 bookmarked

Using the .NET Library and XML in VB6

Rate me:
Please Sign up or sign in to vote.
3.72/5 (9 votes)
11 Mar 2008CPOL4 min read
How to use the .NET Library and XML in VB6.

Image 1


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.


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.

// Read Data
TxtServerName.Text = Configure.getSetting("DBServer");
//Save data
Configure.writeSetting("DBServer", TxtServerName.Text, false);
//Read data from .net in vb6
Dim x As New Configuration.configure
TxtDBServer.Text = CStr(x.getSettingFromVB6("DBServer"))

Image 2

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.

  1. Create a new Windows Control Library project from within Visual Studio.
  2. Image 3

  3. In both the Debug and Release modes of the Property Pages, set the "Register for COM Interop" checkbox.
  4. Inside the AssemblyInfo.cs file, change the assembly wide attribute ComVisible to true. If it's not already in the configuration file, add it.
  5. C#
    [assembly: ComVisible(true)]

That is all that is required to make the project visible to VB6 projects.


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.

Image 4

Once that has been accomplished, then the following code can be added to the form's code in the project.


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.


  • March 10, 2008 - Original article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Portugal Portugal
My name is Nelson Souto, I develop software for fun and profit since 1992.

Comments and Discussions

Generalpassword Pin
Donsw6-Oct-10 12:22
Donsw6-Oct-10 12:22 
AnswerRe: password Pin
Nelson Kosta Souto8-Oct-10 2:41
professionalNelson Kosta Souto8-Oct-10 2:41 
GeneralRe: password Pin
Donsw12-Oct-10 4:23
Donsw12-Oct-10 4:23 
GeneralVery useful Pin
Rafael Nicoletti11-Mar-08 8:13
Rafael Nicoletti11-Mar-08 8:13 
GeneralRe: Very useful Pin
Nelson Kosta Souto11-Mar-08 8:45
professionalNelson Kosta Souto11-Mar-08 8:45 
GeneralRe: Very useful Pin
lincyang3-Aug-09 16:28
lincyang3-Aug-09 16:28 
i have a problem.
in my vb6 project,
i can't run the project,
but the exe file run fine .
if u have time ,help me .
thank u!!
my msn or email
AnswerRe: Very useful Pin
Nelson Kosta Souto4-Aug-09 1:03
professionalNelson Kosta Souto4-Aug-09 1:03 
GeneralRe: Very useful Pin
lincyang4-Aug-09 23:19
lincyang4-Aug-09 23:19 
GeneralRe: Very useful Pin
Nelson Kosta Souto5-Aug-09 23:26
professionalNelson Kosta Souto5-Aug-09 23:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.