C# - File And FileInfo Class

File And FileInfo Class

The .NET Framework class library contains two similar classes for dealing with files — FileInfo and File.

The File class provides static methods for creating, deleting, and manipulating files, whereas the FileInfo class exposes instance members for files manipulation.

Like the Directory class, the File class only exposes static methods and does not contain any properties.

Consider the following program, which creates, deletes, copies, renames, and sets attributes in files, using the File class:

using System;
using System.IO;
namespace WorkingWithFile
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"C:\New Folder\textfile.txt";
            string fileCopyPath = @"C:\New Folder\textfile_copy.txt";
            string newFileName =@"C:\New Folder\textfile_newcopy.txt";
            try
            {
                //---if file already existed---
                if (File.Exists(filePath))
                {
                    //---delete the file---
                    File.Delete(filePath);
                }
                //---create the file again---
                FileStream fs = File.Create(filePath);
                fs.Close();
                //---make a copy of the file---
                File.Copy(filePath, fileCopyPath);
                //--rename the file---
                File.Move(fileCopyPath, newFileName);
                //---display the creation time---
                Console.WriteLine(File.GetCreationTime(newFileName));
                //---make the file read-only and hidden---
                File.SetAttributes(newFileName, FileAttributes.ReadOnly);
                File.SetAttributes(newFileName, FileAttributes.Hidden);
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
    }
}

This program first checks to see if a file exists by using the Exists() method. If the file exists, the program deletes it using the Delete() method. It then proceeds to create the file by using the Create() method, which returns a FileStream object (more on this in subsequent sections). To make a copy of the file, you use the Copy() method. The Move() method moves a file from one location to another. Essentially, you can use the Move() method to rename a file. Finally, the program sets the ReadOnly and Hidden attribute to the newly copied file.

In addition to the File class, you have the FileInfo class that provides instance members for dealing with files. Once you have created an instance of the FileInfo class, you can use its members to obtain more information about a particular file.

Reading and Writing to Files

The File class contains four methods to write content to a file:

WriteAllText() Creates a file, writes a string to it, and closes the file
AppendAllText() Appends a string to an existing file
WriteAllLines() Creates a file, writes an array of string to it, and closes the file
WriteAllBytes() Creates a file, writes an array of byte to it, and closes the file

The File class also contains three methods to read contents from a file:

ReadAllText() Opens a file, reads all text in it into a string, and closes the file
ReadAllLines() Opens a file, reads all the text in it into a string array, and closes the file
ReadAllBytes() Opens a file, reads all the content in it into a byte array, and closes the file