BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 1.1+

Basic File Operations

When an application stores information within files, it is common to include some simple file management tasks within the software. This article describes methods of copying, moving, renaming and deleting files using C# and the .NET framework.

File Operations

When developing software that stores information in files, you may wish to include some basic file management facilities so that the user can work with files without switching to Windows Explorer or a similar tool. This is particularly common if your software works with a large number of files that may be distributed across the file system or accessed remotely. For example, a photo-editing utility may include a gallery containing images that are each a single file. In this situation it is useful for the user to be able to copy, move, rename or delete those files from within the gallery.

The .NET framework includes two classes in the System.IO namespace that allow you to easily perform the four aforementioned file operations. The first is the File class, which contains a set of static methods. This class is ideal when you wish to perform single actions individual files. If you wish to perform multiple actions on a file, the FileInfo class can be more efficient. Instances of this class are linked to the file being processed and can reduce the need for security checks between calls.

In this article we will examine the methods of both File and FileInfo. To begin, create a new project and ensure the following using directive in the class that you will be using:

using System.IO;

Copying a File

The first of the four common file operations that we will examine is file copying. When using the File class, there are two overloads of the Copy method. The first accepts two string parameters containing the path and the file name of the file to be copied and the path and name for the copy. The following example copies a file named "Test.txt" that resides in the folder "c:\temp". The copy of the file has a new name but is created in the same folder in this sample:

File.Copy("c:\\temp\\Test.txt", "c:\\temp\\Copy.txt");

When using this version of the method, an exception is thrown if the target file already exists. You can check the existence of the file before copying but, as it is possible that another process will create the file between checking and copying, you should always use a try/catch block and recover gracefully from anticipated exceptions.

A second overload includes a Boolean parameter. If set to false, the method is identical to the previous sample. However, if you pass true to the parameter, the method will force copying, overwriting an existing file if required.

File.Copy("c:\\temp\\Test.txt", "c:\\temp\\Copy.txt", true);

When working with the FileInfo class, you must first create an instance and link it to the file to be processed. This is achieved by passing the name of the file as a parameter to the FileInfo constructor:

FileInfo file = new FileInfo("c:\\temp\\Test.txt");

With the object instantiated, you can copy the file using the CopyTo method. This method includes two overloaded versions that are similar to the File class's Copy method. As the object is linked to a file, the first parameter is not needed. Instead, only the target file name is specified. Again, a Boolean flag can be included that determines whether the operation may overwrite an existing file.

file.CopyTo("c:\\temp\\Copy2.txt");
file.CopyTo("c:\\temp\\Copy2.txt", true);

The CopyTo method differs from the Copy method in that it returns a FileInfo object. The retuned object is linked to the file that was created by the copying. This can be used for further copying or other actions.

FileInfo copy = file.CopyTo("c:\\temp\\Copy3.txt");
copy.CopyTo("c:\\temp\\Copy4.txt");

NB: The above examples all use absolute file paths. It is also possible to use relative paths and UNC paths, where these are supported.

Moving a File

Files can be moved from one location to another, optionally changing their file name at the same time, using the Move method of the File class. This method accepts string arguments for the file to move and the new location and name. The following example moves the test file from the temp folder to a sub-folder named "subfolder" and changes its name to "Moved.txt". The target folder must exist for the operation to be successful. The method will also fail with an exception if the target file already exists.

File.Move("c:\\temp\\Test.txt", "c:\\temp\\subfolder\\Moved.txt");

The FileInfo class provides the MoveTo method to allow files to be moved. This method does not return a value. Instead, the existing FileInfo object is updated to refer to the new location and file name.

FileInfo file = new FileInfo("c:\\temp\\subfolder\\Moved.txt");
file.MoveTo("c:\\temp\\Test.txt");

Renaming a File

Neither the File class nor the FileInfo class provide a method for renaming a file. However, the Move and MoveTo methods can be used for this purpose. Instead of specifying a new folder for the moved file, the same path is used for both parameters with only the file name part being changed:

File.Move("c:\\temp\\Test.txt", "c:\\temp\\Renamed.txt");

For the FileInfo class, use the MoveTo method as follows:

FileInfo file = new FileInfo("c:\\temp\\Renamed.txt");
file.MoveTo("c:\\temp\\Test.txt");

Deleting a File

The last of the four basic operations is the deleting of files. The File class include the static Delete method to allow a file to be permanently erased. This method does not send the file to the recycle bin and gives no option for cancelling the operation, so should be used with care.

The File class's method accepts a single parameter containing the name of the file to be deleted.

File.Delete("c:\\temp\\Test.txt");

The FileInfo class also contains a Delete method. As the file name is set in the constructor, the method requires no parameters.

FileInfo file = new FileInfo("c:\\temp\\Copy.txt");
file.Delete();
22 December 2009