C# Files


This entry is part 1 of 4 in the series C# Files Directories

Working with files and directories is perhaps one of the easier things to do in C#. Because files are used for input an output the file classes are contained in the System.IO directory. There are few classes we will likely be working with, File, FileInfo, Directory, DirectoryInfo and Path. The namespace System.IO has these classes, so you need to add using.

Files

File and FileInfo both contain classes for copying, moving, deleting, opening and creating files. The difference is that FileInfo provides instance methods and File provides static methods. This means that when you are using FileInfo you need to instantiate an object for you can use it. Every time you call the static method the OS does some security checking to see if the application has the correct privileges to access the directory and file.

Here is a short list of some of the most useful methods available.

  • Create()
  • Copy()
  • Delete()
  • Exists()
  • GetAttributes()
  • Move()
  • ReadAllText()

Here is some example code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;                      // add this 

namespace FileManipulation
{
    class Program
    {   // File (statis) and FileInfo (instance)
        static void Main(string[] args)
        {
            // First, create a file: D:\temp\myfile.txt

            var filenamewithpath = @"D:\temp\myfile.txt";  // verbatim @
            //var path = @"D:\temp\";
            File.Copy(filenamewithpath, @"D:\temp\myfile_2.txt", true); // true will over-write existing file
            File.Copy(filenamewithpath, @"D:\temp\myfile_3.txt", true);
            Console.WriteLine("File copied");
            File.Delete(@"D:\temp\myfile_3.txt");

            if(File.Exists(@"D:\temp\myfile_2.txt"))
            {
                Console.WriteLine("The file " + @"D:\temp\myfile_2.txt" + " exists.");
            }

            string filecontent = File.ReadAllText(filenamewithpath);

            var fileInfo = new FileInfo(filenamewithpath);
            fileInfo.CopyTo(@"D:\temp\myfile_4.txt", true);

            var fileInfo4 = new FileInfo(@"D:\temp\myfile_4.txt");

            if (fileInfo4.Exists)  // Exists is a property
            {
                fileInfo4.Delete();  // takes no paramters
            }
            else
            {
                Console.WriteLine("Cannot delete file " + @"D:\temp\myfile_4.txt" + " because it does not exist.");
            }
            // FileInfo does not have a ReadAllText method
            // need to call openread which returns a file string but 
            // that is a little bit complex.

            Console.WriteLine("Use File for occasional usage and FileInfo for many operations");
            Console.WriteLine("because each time you use File the OS does security checks and that");
            Console.WriteLine("can slow down you app, with FileInfo you need to create an instance");
            Console.WriteLine("of it. Both are easy to use anyway.");
            Console.WriteLine("Press any key to contine...");
            Console.ReadKey();
        }
    }
}
Series NavigationC# Write File with Encoding >>