File.Move() Method in C# with Examples

21 Apr, 2020
File.Move() is an inbuilt File class method that is used to move a specified file to a new location. This method also provides the option to specify a new file name.
Syntax:
public static void Move (string sourceFileName, string destFileName);
Parameter: This function accepts two parameters which are illustrated below:
sourceFileName: This is the specified source file which are going to be moved. It can be absolute or relative path.
destFileName: This is the specified destination file where source file is going to be moved.
Exceptions:
IOException: The destFileName already exists.
FileNotFoundException: The sourceFileName was not found.
ArgumentNullException: The sourceFileName or destFileName is null.
ArgumentException: The sourceFileName or destFileName is a zero-length string, contains only white space, or invalid characters as defined in InvalidPathChars.
UnauthorizedAccessException: The caller does not have the required permission.
PathTooLongException: The given path, file name, or both exceed the system-defined maximum length.
DirectoryNotFoundException: The path specified in sourcefilename or destFileName is invalid.
NotSupportedException: The sourceFileName or destFileName is in an invalid format.
Below are the programs to illustrate the File.Move() method.
Program 1: Before running the below code, a file file.txt is created with some contents shown below-
// C# program to illustrate the usage// of File.Move() method  // Using System and System.IO namespacesusing System;using System.IO;  class GFG {    static void Main()    {        try {            // Moving the file file.txt to location C:\gfg.txt            File.Move(@"file.txt", @"C:\gfg.txt");            Console.WriteLine("Moved");        }        catch (IOException ex) {            Console.WriteLine(ex);        }    }}
Output:
Moved

After running the above code, above output is shown and the existing file file.txt moved to a new location C:\gfg.txt which is shown below-
Program 2: Initially no file was created.
// C# program to illustrate the usage// of File.Move() method  // Using System and System.IO namespacesusing System;using System.IO;  class GFG {    static void Main()    {        try {            // If file.txt is not found            // then an exception will be shown            File.Move(@"file.txt", @"C:\gfg.txt");            Console.WriteLine("Moved");        }        catch (IOException ex) {            Console.WriteLine(ex);        }    }}
Runtime Error:
System.IO.FileNotFoundException: Could not find file ‘/home/runner/NutritiousHeavyRegression/file.txt’.File name: ‘/home/runner/NutritiousHeavyRegression/file.txt’at System.IO.File.Move (System.String sourceFileName, System.String destFileName) [0x0007c] in :0at GFG.Main () [0x00000] in :0
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# | Multiple inheritance using interfaces
Differences Between .NET Core and .NET Framework
Extension Method in C#
C# | List Class
HashSet in C# with Examples
C# | .NET Framework (Basic Architecture and Component Stack)
Switch Statement in C#
Partial Classes in C#
Lambda Expressions in C#
Hello World in C#