C# File.Move Method, Rename File

This C# example uses the File.Move method from the System.IO namespace.
File.Move renames a file. It is a tested .NET Framework method. The File class in the System.IO namespace provides this convenient method. It performs a fast rename of the file you target. It sometimes throws exceptions.
Example. The method that renames files is called File.Move. You must include the System.IO namespace at the top with a using directive or specify the fully qualified name "System.IO.File.Move".String Literal

Here: The program has different results depending on whether the source file exists, and whether the target file already exists.

Info: The program includes the System namespace and the System.IO namespace so that File, Console and IOException can be used.

Next: File.Move uses system routines to attempt to change the name of the first file to the name of the second file.

So: If successful, the first file will no longer exist. If unsuccessful, the operation will be terminated—nothing will be changed on disk.

C# program that renames files with File.Move using System; using System.IO; class Program { static void Main() { // // Move a file found on the C:\ volume. // If the file is not found (SAM.txt doesn't exist), // then you will get an exception. // try { File.Move(@"C:\SAM.txt", @"C:\SAMUEL.txt"); // Try to move Console.WriteLine("Moved"); // Success } catch (IOException ex) { Console.WriteLine(ex); // Write error } } } Output System.IO.FileNotFoundException: Could not find file 'C:\SAM.txt'. File name: 'C:\SAM.txt' at System.IO.__Error.WinIOError...
Exceptions. Whenever you are dealing with the file system, errors will occur. You must be prepared to recover from these errors (even if recovery means exiting). It is usually a mistake not to use exception handling around methods like File.Move.

Info: If the "SAMUEL.txt" file on the C volume already exists, you will get another exception.

Tip: To solve this problem, you can check the target path with the File.Exists method before attempting the File.Move.

File.ExistsIOException
System.IO.IOException: Cannot create a file when that file already exists.
Uses. We mention some uses of File.Move. We determine when it is preferable to File.Copy and other approaches. Internally, File.Move uses the Win32Native.MoveFile function, which does a rename on the file system.
If you use File.Copy, you will actually be copying the data on the disk, which will be more resource-intensive and slower. However, you should avoid File.Move if you need to retain two copies of the data.
Summary. We renamed files in using the .NET Framework's File.Move method. We looked at the usage of this method on a file that exists and does not exist, and then noted some exceptions caused by this method.

Finally: We peeked inside the .NET Framework's implementation of File.Move and mentioned the difference between File.Copy and File.Move.

© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls