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 Create File and Append text in files using “FILE CLASS” using SYSTEM.IO in c# (.net 2.0).

Author: Zunnair
Download Source Code : 836_CreateNApend.zip

In this simple article you will learn how to create a file and open it directly for writing by using CreateText() function and how can you append text in file by using AppendAllText() function of FILE

 File class provide us two static functions to create a file. One which I have discussed in my previous article that function creates file. Here we will discuss that how we can create a file and directly open it for writing by using CreateText() function.

As I told that this function is static so we can directly call it by using class name.

CreateText() function takes file path as a string and create a text file at a given location. It returns streamwriter class object.

To demonstrate make a window application. Drag one text box and one button on the form. Write file path (like c:\\abc.txt) in the first textbox and press button.

Now write the following code on button click event:

C#

private void btncreate_Click(object sender, EventArgs e)

        {

          

             str=txtPath.Text;

            if (str != null)

            {

               StreamWriter sr = File.CreateText(str);

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

                {

                    MessageBox.Show("file created");

                }

                else

                    MessageBox.Show("INVALID PATH");

 

            }

            else

                MessageBox.Show("Please wite Path");                     

        }

VB

Private Sub btncreate_Click(ByVal sender As Object, ByVal e As EventArgs)

        Str = txtPath.Text

        If str IsNot Nothing Then

            Dim sr As StreamWriter = File.CreateText(str)

            If File.Exists(str) = True Then

                MessageBox.Show("file created")

            Else

                MessageBox.Show("INVALID PATH")

            End If

 

        Else

            MessageBox.Show("Please wite Path")

        End If

  End Sub

 

So this function gives us a benefit that we do not have to open file after creating it is already opened by function.

APPEND TEXT DIRECTLY:

In my previous articles I discussed different functions to write text in files but all the functions overwrite the previous data. But if we wanted to add some data with previous data first we had to open file in append mode.

Now we will see that how we can add data in the previous data directly by using AppendAllText() function. we do not need to open file in append mode, this functionl opens the file in append mode by it self. It also provides us a facility that It creates a file by itself if file does not exist at the given location.

AppendAllText() takes file path and text, which user want to write, 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.AppendAllText(str, txtwrite.Text);

 

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

                {

                    MessageBox.Show("text written 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.AppendAllText(str, txtwrite.Text)

 

            If File.Exists(str) = True Then

                MessageBox.Show("text written WRITTEN")

            Else

                MessageBox.Show("INVALID PATH NO FILE")

            End If

        Else

            MessageBox.Show("Please wite Path")

        End If

    End Sub

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 learn how to create a file and open it directly for writing by using CreateText() function and how can you append text in file by using AppendAllText() function of FILE class

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Close active form in a MDI container using C# and VB in .net 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net