Author: Zunnair
Download Source Code : 845_io.zip
In this simple article you will learn how to read multiple text lines from a file and write multiple text lines in a file using FILE class in C# .net 2.0.
In my previous article I discussed that how can you create, open and close a file using “FILE CLASS” (SYSTEM.IO).
We can directly read multiple text lines from or write multiple text in file by using the file class functions.
READ MULTIPLE TEXT LINES FROM FILE:
First of all create a file as I told in my previous article and write some text lines in it. File class provides us static function to read multiple text lines from a file so we do not need to create an object of “File Class”. To read from a file we have “ReadAllLines()” function of FILE CLASS.
When we want to read from a file first of all we have to open it but ReadAllLines() function provide us a facility that It opens file by it self so we do not need to open it, we can directly call this function.
ReadAllLines() function takes file path as a parameter and returns text lines (read from the file) as a string array.
To demonstrate make a window application. Drag two text boxes and one button on the form. Write file path (like c:\\abc.txt) in the first textbox and press button. Text read from the file will come in second text box.
Now write the following code on button click event:
C#
private void btnread_Click(object sender, EventArgs e)
{
str = txtPath.Text;
if (str != null)
{
if (File.Exists(str) == true)
{
string [] ab= File.ReadAllLines(str);
for (int i = 0; i < ab.Length; i++)
MessageBox.Show(ab[i]);
}
else
MessageBox.Show("INVALID PATH NO FILE");
}
else
MessageBox.Show("Please wite Path");
}
VB
Private Sub btnread_Click(ByVal sender As Object, ByVal e As EventArgs)
str = txtPath.Text
If str IsNot Nothing Then
If File.Exists(str) = True Then
Dim ab As String() = File.ReadAllLines(str)
For i As Integer = 0 To ab.Length - 1
MessageBox.Show(ab(i))
Next
Else
MessageBox.Show("INVALID PATH NO FILE")
End If
Else
MessageBox.Show("Please wite Path")
End If
End Sub
As I told that ReadAllLines() opens file by it self but I would like to tell that it also close file by it self so we do not have to close it by our self.
WRITE MULTIPLE TEXT LINES IN FILE:
Again File class provides us static function to write multiple text lines in a file so we do not need to create an object of “File Class”. To write in a file we have “WriteAllLines()” function of FILE CLASS.
To read from a file there should be a file and text in a file. But “WriteAllLines()” function provides us a facility. It creates a file if file does not exist at the given path. But if file exists than it is overwritten.
When we want to write in a file first of all we have to open it but WriteAllLines() function provide us a facility that It opens file by it self so we do not need to open it, we can directly call this function.
WriteAllLines() function takes file path and string array (which should be written in file) as a parameter. it returns nothing.
To demonstrate make a window application. Drag two text boxes and one button on the form. Write file path (like c:\\abc.txt) in the first textbox and write text in the second textbox than press button to write in file.
Now write the following code on button click event:
C#
private void btnwrite_Click(object sender, EventArgs e)
{
str = txtPath.Text;
if (str != null)
{
string [] st=new string[5];
st[0]="devasp.net";
st[1]="devasp.net";
st[2] = "devasp.net";
st[3] = "devasp.net";
st[4] = "devasp.net";
File.WriteAllLines(str,st);
if (File.Exists(str) == true)
{
MessageBox.Show("lines WRITTEN");
}
else
MessageBox.Show("INVALID PATH NO FILE");
}
else
MessageBox.Show("Please wite Path");
}
VB
Private Sub btnwrite_Click(ByVal sender As Object, ByVal e As EventArgs)
str = txtPath.Text
If str IsNot Nothing Then
Dim st As String() = New String(4) {}
st(0) = "devasp.net"
st(1) = "devasp.net"
st(2) = "devasp.net"
st(3) = "devasp.net"
st(4) = "devasp.net"
File.WriteAllLines(str, st)
If File.Exists(str) = True Then
MessageBox.Show("lines WRITTEN")
Else
MessageBox.Show("INVALID PATH NO FILE")
End If
Else
MessageBox.Show("Please wite Path")
End If
End Sub
As I told WriteAllLines() opens file by it self but I would like to tell that it also close file by it self so we do not have to close it by our self.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Devasp.net FILE HANDLING Application";
}
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Me.Text = "Devasp.net FILE HANDLING Application"
End Sub
This simple article tells you how to read multiple text lines from a file and write multiple text lines in a file.