Login | Sign Up | iGold Site Search
iGold Logo
  iGold Home
Home
iGold Blog
Blog
iGold InterView
Interview
iGold Articles
Articles
iGold Tips
Tips
iGold Tips
Tools
iGold Tips
Forum
iGold Tips
Feedback
Skip Navigation LinksiGold Home : Articles : View Article

 
bottomLeftImg   bottomRightImg
iGold new

Dot Net Interview Questions !!!
 
Interview Tips
 
on iGold.in

bottomLeftImg   bottomRightImg
PixImg

Links
 
bottomLeftImg   bottomRightImg
Login
New User SignUp
Change Password
Forgot Password

Forums
Asp.Net
Csharp
XML,XSLT
SQL Server
bottomLeftImg   bottomRightImg
PixImg
 

How to Read and Write an INI file

Author :   Ashok Raja .T Dated   :  21-Mar-2007

How Read and Write an INI file

Introduction

 

INI files are text files with various configuration options set into sections. The sections are usually declared in square brackets (e.g., [My Section]) followed by single lines listing the various configuration options

 

[My Section]

Mykey=Welcome to www.igold.in

 

Read from INI File

 

There is no direct class in dotnet to communicate with INI files. One method is performing a normal File IO operation. Which is not advisable due to the complexity in extracting the sections and keys. The preferable method is to access the file through GetPrivateProfileString function available in Kernel32.dll. This dll has to be imported to avail this function.

 

������� /// <summary>

������� /// Declare the API

������� /// </summary>

������� [DllImport("kernel32")]

������� private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retStr, int bufferSize, string filePath);

�������

section ��������� � The Section in INI file which contains the key.

key ��� ��������� -The name of the key whose associated string is to be retrieved

def���� ��������� - Default value to be displayed if no associated key is found.

retStr��������� - A string builder which returns the value of key

buffersize��������� - The size of the buffer required by retStr ( string Builder)

filepath��������� - The location of INI file.

 

The namespace System.Runtime.InteropServices has to be imported to use DllImport attribute and System.Text is required for StringBuilder Class

 

This following code snippet retrieves value of MyKey key, which is placed under section My Section.

 

StringBuilder outStr = new StringBuilder(255);

 

GetPrivateProfileString("My Section", "Mykey", "", outStr, 255, Application.StartupPath + "\\settings.ini");

 

����������� Console.WriteLine(outStr);

 

OutStr returns �Welcome to www.igold.in� .

 

Write to INI File

 

Function WritePrivateProfileString available in Kernel32.dll is required to write values to an INI file.

 

������� [DllImport("kernel32")]

������� private static extern long WritePrivateProfileString(string section,string key, string val, string filePath);

 

 

The variable name itself speaks its usage. This function can be called in the under mentioned way.

 

WritePrivateProfileString("My New Section", "My New Key", "New Value", Application.StartupPath + "\\settings.ini");

 

Conclusion

 

The most preferred method in dotnet to handle configuration settings is through .config files. This article is intended for users to handle legacy INI configuration file.

 


For more details contact info@igold.in , Copyright © iGold.in 2004-2006
Terms of Use  | Privacy Statement

Asp.Net 2.0 Logo