How to read Values from ini File in C#


Read Values from Configuration file i.e ini files in C#

This is a code to read ini file.
Steps to be followed:
1.First Create a ini file with .ini extention.
2.Copy this piece of text in emp.ini file
3.Save it as C:\\emp.ini


Copy this text as it is.
[iniexample]
ABC=4
Version=1
XYZ=10

Now in Visual Studio Open a Windows Application in C#.
Copy this Code to read Values

public void read()
{
string line;
string deviceName = string.Empty;
// Read the file and display it line by line.
StreamReader file = new

System.IO.StreamReader("c:\\emp.ini");
{
while ((line = file.ReadLine()) != null)
{
if (line.ToLower().StartsWith("xyz"))
{
string[] fullName = line.Split('=');
deviceName = fullName[1];
break;
}
}
}
label1.Text = deviceName;

}
//in Button Click Event
//call the read Method.
//read();

Thats all.


Comments

Author: amol00725 Jan 2010 Member Level: Silver   Points : 0

Nice code..

Thanks & Regards
Amol

Guest Author: Jeanette09 Dec 2011

Good point. I hadn't tough about it quite that way.



  • 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: