Reading Configuration Files Without Using Win32 | CodeGuru

Reading Configuration Files Without Using Win32

A C# Configuration File Reader that doesn’t use the Win32 API functions. . Environment: .Net, C# This is just a small class to handle configuration files. There are several other classes like this one, but they all use the GetPrivateProfileString()/SetPrivateProfileString() family of functions from the Win32 API. If you check the documentation for those API, […]

Written By
CodeGuru Staff
CodeGuru Staff
Jun 13, 2002
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

A C# Configuration File Reader that doesn’t use the Win32 API functions.

.

Environment: .Net, C#

This is just a small class to handle configuration files. There are several other classes like this one, but they all use the GetPrivateProfileString()/SetPrivateProfileString() family of functions from the Win32 API.

If you check the documentation for those API, you’ll notice that they are provided merely for compatability with Win16 applications. It’s been quite some time since anyone wrote Win16 applications. (At least, I hope so.) You can find a C# class that uses those API here, using Interop. I don’t like to use compatability functions; Microsoft is usually good with compatability, but still… and when you add Interop to the mess, I decided to do it on my own.

So I wrote a class that does it for me, without using Interop. It uses the same syntax as all INI files for Windows, and it supports sections and section-less keys.

Configuration conf =
 new Configuration(new System.IO.StreamReader("Configuration.ini"));
string UserName = conf.GetValue("User");
string HomePage = conf.GetValue("Internet_Data","Home_Page");

conf.SetValue( "User_Information",
               "Log_In_Time",
               System.DateTime.Now.ToString());

Pretty simple, isn’t it? Now, it’s not XML, and it doesn’t have hype all over it, but it’s small, efficent, and allows data hiding. If you want to switch the back end from an INI file to xn XML file, it’s just a matter of changing the Parse() & Save() methods. The code is well commented, you shouldn’t have any trouble with it. If you do have problems, I’ll be glad to hear about them.

Downloads

Download demo project – 5.67 Kb
Download source – 3.61 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.