Working With File Class in C#


Introduction:

This article will explain about the File class and its operations.  The System.IO namespace is one of the most significant namespaces used for working with files in the .Net Framework. Let us see about the class. 


File Class 

The File class exposes many static methods for moving, copying, and deleting files. There are static methods that involve moving a file, copying and deleting a file. 

Some of the most useful static methods of the file class are:

S.No Method Description
1 Copy This method is used to copy a file to the specified location.
2 Create This method is used to create a file in the specified path.
3 Delete This method is used to Delete a file.
4 Open This method is used to return a filetream object at the specified path.
5 Move Moves a specified file to a new location. we can specify a different name for the file in the new location.
6 Exists Determines whether the specified file exists.
7 OpenRead Opens an existing file for reading.
8 OpenWrite Opens an existing file or creates a new file for writing.

Create Method:

This method is used to create a file in the specified path.

string path = @"D:\MyTestFile1.txt";

FileStream fs = File.Create(path);

{

Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");             

fs.Write(info, 0, info.Length);

Console.WriteLine("File has been created");

}    

 

Delete Method

 

This method is used to Delete a file.

 

static void Main(string[] args)

        {

            string path = @"D:\MyTest1.txt";                    

                File.Delete(path);

            Console.WriteLine ("File has been deleted");   

        }

 
Open Method 


This method is used to return a FileStream object at the specified path. 


Exists Method 


Determines whether the specified file exists.


static void Main(string[] args)

{

 string path = @"D:\MyTestFile1.txt";

 Console.WriteLine(File.Exists(path) ? "File exists." : "File does not exist.");

 }


Copy Method 


This method is used to copy a file to the specified location.


static void Main(string[] args)

   {

            string path = @"D:\MyTestFile1.txt";

            string path1 = @"D:\MyTest1.txt";

            File.Copy(path, path1); 

        Console.WriteLine ("File has been copied");   

   }


Move Method 


Moves a specified file to a new location. We can specify a different name for the file in the new location.


static void Main(string[] args)

        {

            string path = @"D:\MyTestFile1.txt";

            string path1 = @"c:\MyTest1.txt";

            File.Move(path, path1);

 Console.WriteLine ("File has been moved");          

        }     

 

OpenRead method

 

Opens an existing file for reading.

 

public static void Main()

        {

            string path = @"d:\MyTest1.txt";

 

            if (!File.Exists(path))

        {

            // Create the file.

            FileStream fs = File.Create(path);

            {

                Byte[] info = new UTF8Encoding(true).GetBytes("This is a file");

                fs.Write(info, 0, info.Length);

            }

        }

     

        using (FileStream fs = File.OpenRead(path))

        {

            byte[] b = new byte[1024];

            UTF8Encoding temp = new UTF8Encoding(true);

 

            while (fs.Read(b,0,b.Length) > 0)

            {

                Console.WriteLine(temp.GetString(b));

            }

        }

 

Conclusion:

The File class is very useful for working with files. If there is any mistake in this article then please notify me. I expect your valuable comments and feedback about this article.

Up Next
    Ebook Download
    View all
    Learn
    View all