Sunday, January 17, 2010

Reading From Text File in C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Reading_From_Flat_fIle
{
public partial class Form1 : Form
{
FileStream Fstream;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
string strPath;
strPath = textBox1.Text;
try
{
if (File.Exists(strPath))
{
Fstream = new FileStream(strPath, FileMode.Open);
}
else
{
IOException ex = new IOException("File Not Found");
throw ex;
}
StreamReader SReader = new StreamReader(Fstream);
string strContent = "";
string strRead = SReader.ReadLine();
while (strRead != null)
{
strContent = strContent + strRead;
strRead = SReader.ReadLine();
}
textBox2.Text = strContent;
SReader.Close();
Fstream.Close();
}
catch (IOException IOEX)
{
MessageBox.Show(IOEX.Message );
}

}
}
}

No comments:

Post a Comment