Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to READ text from and WRITE text in files using “FILE CLASS” in c# (.net 2.0).

Author: Zunnair
Download Source Code : 844_io.zip

In this simple article you will learn how to read text from a file and write text 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 text from or write text in file by using the file class functions.

READ TEXT FROM FILE:

First of all create a file as I told in my previous article and write some data in it. File class provides us static function to read text from a file so we do not need to create an object of “File Class”. To read from a file we have ReadAllText() function of FILE CLASS.

When we want to read from a file first of all we have to open it but ReadAllText() 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.

ReadAllText() function takes file path as a parameter and returns text (read from the file) as a string.

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)

                {                 

                    txtread.Text = File.ReadAllText(str);

                }

                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

                txtread.Text = File.ReadAllText(str)

            Else

                MessageBox.Show("INVALID PATH NO FILE")

            End If

 

        Else

            MessageBox.Show("Please wite Path")

        End If

    End Sub

As I told that ReadAllText() 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 TEXT IN FILE:

Again File class provides us static function to write text in a file so we do not need to create an object of “File Class”. To write in a file we have WriteAllText () function of FILE CLASS.

To read from a file there should be a file and text in a file. But “WriteAllText () 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 WriteAllText () 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.

WriteAllText () function takes file path and string (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)

            {

                File.WriteAllText(str, txtwrite.Text);

                if (File.Exists(str) == true)

                {

                    MessageBox.Show("TEXT 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

            File.WriteAllText(str, txtwrite.Text)

            If File.Exists(str) = True Then

                MessageBox.Show("TEXT WRITTEN")

            Else

                MessageBox.Show("INVALID PATH NO FILE")

            End If

 

        Else

            MessageBox.Show("Please wite Path")

        End If

 End Sub

As I told WriteAllText () 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 text from a file and write text in a file

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Read bytes from and Write bytes in files using “FILE CLASS” using system.io in c# (.net 2.0).

Disclaimer - Privacy
© 2002-2017 DevASP.net