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 perform create, open and close operations on files using “FILE CLASS” using SYSTEM.IO Namespace in c#.net 2.0.

Author: Zunnair
Download Source Code : 832_io.zip

In this simple article you will learn how to create open and close a file using FILE class in C# .net 2.0

1. Create A New File:

You can create a new file by using file class function create. It is a static function so you do not need to create an object. To create a file you will have to give file path as a string with extension (like txt, dat etc).

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 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)

            {

                File.Create(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

            File.Create(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

Open A File:

To open an existing file you will have to use open functions of file class. File class have different open functions.

1.       Open

This is a simple open function it takes the path of existing file and opening mode.

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 textbox and press button.

Now write the following code on button click event:

C#

private void btnopen_Click(object sender, EventArgs e)

        {

            str = txtPath.Text;           

            File.Open(str, FileMode.Append); 

        }

VB

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

        str = txtPath.Text

        File.Open(str, FileMode.Append)

End Sub

2.       Open Text File:

To open a text file you can use openText() function of a FILE  class. It takes a file path and opens it.

Now write the following code on button click event:

C#

private void btnopen_Click(object sender, EventArgs e)

        {

            str = txtPath.Text;

            File.OpenText(str);              

        }

VB

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

        str = txtPath.Text

        File.OpenText(str)

End Sub

3)       Open For Write:

To open a file directly in write mode you can use openWrite() function of a FILE  class. It takes a file path and opens it in write mode.

Now write the following code on button click event:

C#

private void btnopen_Click(object sender, EventArgs e)

        {

            str = txtPath.Text;

            File.OpenWrite(str);

        }

VB

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

        str = txtPath.Text

        File. OpenWrite(str)

    End Sub

4)       Open For Read:

To open a file directly in read mode you can use openRead() function of a FILE class. It takes a file path and opens it in read mode.

Now write the following code on button click event:

C#

private void btnopen_Click(object sender, EventArgs e)

        {

            str = txtPath.Text;

            File.OpenRead(str);

        }

VB

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

        str = txtPath.Text

        File. OpenRead(str)

    End Sub

 

 

Close A File:

To close a file you have to use FileStream class function. File class does not provide you any function to close a file.

Now write the following code on button click event:

C#

private void btn_close_Click(object sender, EventArgs e)

        {

            str = txtPath.Text;

            FileStream obj = new FileStream(str, FileMode.Append);

            obj.Close();

      MessageBox.Show("FILE CLOSED");

  }

VB

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

        str = txtPath.Text

        Dim obj As New FileStream(str, FileMode.Append)

        obj.Close()

        MessageBox.Show("FILE CLOSED")

    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 you how to create open and close a file using FILE class in C# .net 2.0.

Article Comments
hey, thats too good, can i also create and read html files using the same method.?

Posted on 3/12/2009 9:42:05 AM by Zain Shaikh

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How To Set Tab Order Of Control On Windows Form in .net 3.5.

Disclaimer - Privacy
© 2002-2017 DevASP.net