Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

File.Move() Method in C# with Examples

  • Last Updated : 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-

file.txt




// 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);
        }
    }
}

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.




// 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);
        }
    }
}

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
Recommended Articles
Page :