Tuesday, January 26, 2010

Reading from Text File in C#

Here is a simple C# segment of code that reads values from a text/ini file. It's from my NetPinger Application. It uses Stream Reader and the system.IO libraries of .NET.

using System.IO;

try

{

FileStream file = new FileStream

(filename,

FileMode.Open);

StreamReader sr = new StreamReader(file);

string skip1 = sr.ReadLine();

AppTitle = sr.ReadLine();

string skip3 = sr.ReadLine();

string skip2 = sr.ReadLine();

connectionString = sr.ReadLine();

string skip4 = sr.ReadLine();

string skip5 = sr.ReadLine();

txtSite.Text = sr.ReadLine();

string skip6 = sr.ReadLine();

string skip7 = sr.ReadLine();

txtSMTP.Text = sr.ReadLine();

string skip8 = sr.ReadLine();

string skip9 = sr.ReadLine();

try

{

txtQuery.Value = Convert.ToDecimal(sr.ReadLine());

}

catch (Exception ex)

{

}

sr.Close();

file.Close();

txtConnect.Text = connectionString;

txtLDAP.Text = connectionString;

this.ResizeMe();

this.Connect();

}

catch (Exception ex)

{

toolStripStatusLabel1.Text = ex.Message;

mnuNotify.ShowBalloonTip

(

5000,

"NetPinger 2010",

ex.Message,

ToolTipIcon.Error

);

}

No comments:

Post a Comment