Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Application Configuration Settings .NET & VB 6 With XML

0.00/5 (No votes)
17 Jun 2008 1  
Application configuration settings .NET & VB 6 with XML

Introduction

This is my first article. I hope that my code and findings are helpful to other members of The Code Project.

INI files and the registry are generally things of the past for .NET applications. But what do we 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.

History

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)
    • Set the path and filename for the XML settings file
  • void WriteSetting(String Key, String KeyValue, bool encode)
    • Write a value to the settings file given a key and value name
    • 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 vb6
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.

Registering the Assembly to Work with VB6

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 Visual Studio 2005 performs this registration automatically while the environment is running, but un-registers it when Visual Studio 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\Framework\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

Conclusion

XML files are a widespread standard that allow easy storage of structured typed data. Use of XML files allow easy editing by end users, and even by other software. Using System.XML, you can easily store your settings in XML files.

Also check this article for more information.

History

  • 7th March, 2008: Initial version
  • 17th June, 2008: Article updated

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here