How to read and write Ini Files in Asp.net.


Today I will try to explain to read and write the ini files in asp.net Programming with the help of source Code. Ini files are used to store for database configuration values such as server name,port,Database e.t.c and System setup configuration Such as Operating system,Version,Disk space,Server ip e.t.c.

Ini files have an extension with .Ini .ini files having a Section name and keyword value.The Below Picture illustrates the exact structure of an inf file.

ini Structure

The Basic Structure or Schema of the .ini files are described as below.


[sectionname]
keyword1=value1
keyword2 =value2
keyword3= value3


Multiple values for a keyword are known as tags Tags are separated by commas like this .


keyword3 = value3,value4


Functions for Reading and writing .ini files .

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Ini
{
///
/// Create a New INI file to store or load data
///

public class IniFile
{
public string path;

[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);
[DllImport("kernel32")]
Private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
///
/// INIFile Constructor.
///

///
public IniFile(string INIPath)
{
path = INIPath;
}
///
/// Write Data to the INI File
///

///
/// Section name
///
/// Key Name
///
/// Value Name
public void IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.path);
}

///
/// Read Data Value From the Ini File
///

///
///
///
///
public string IniReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path);
return temp.ToString();
}
}
}


Write the above methods in a class Library and build it.And add the dll to the Application.From the Applications we need to call these functions to call IniWriteValue


After Importing the dll the Ini file has a parameter Constructor in which you can pass the path file.As discussed above the ini has a Section and a keyword and keyvalue the below is the way to write .ini file


IniFile ini = new IniFile("C:\\test.ini");
ini.IniWriteValue("Info","Name","Bhushan");
ini.IniWriteValue("Info","LastName",""kambampati");





After Importing the dll the Ini file has a parameter Constructor in which you can pass the path file.As discussed above the ini has a Section and a keyword ,keyvalue will be written in the below way to read the .ini file

Reading Ini file
Reading an ini file


IniFile ini = new IniFile("C:\\Users\\Bhushan\\Documents\\Visual Studio 2010\\Projects\\UserTraficReports\\SynapseReport\\SynapseReport\\Express\\DVD-5\\DiskImages\\DISK1\\Setup.ini");
strInfo = ini.IniReadValue("Info", "Name");
strName = ini.IniReadValue("Info", "Version");
strDiskSpace = ini.IniReadValue("Info", "DiskSpace");
strconnection = ini.IniReadValue("Connection", "SeverIp");


Attachments

Article by srirama
A Good advice from parent to a Child , Master to a Student , Scholar to an Ignorant is like a doctor prescribed pill it is bitter to take but when they take it will do all good for them --- Bhushan

Follow srirama or read 73 articles authored by srirama

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: