File.Move() Method in C# with Examples

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-

file.txt

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the usage
// of File.Move() method
  
// Using System and System.IO namespaces
using 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);
        }
    }
}

chevron_right


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-

C:\gfg.txt

Program 2: Initially no file was created.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the usage
// of File.Move() method
  
// Using System and System.IO namespaces
using 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);
        }
    }
}

chevron_right


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 :0
at GFG.Main () [0x00000] in :0




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.