Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

File.Move Method

Moves a specified file to a new location, providing the option to specify a new file name.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public static void Move(
	string sourceFileName,
	string destFileName


Type: System.String
The name of the file to move.
Type: System.String
The new path for the file.


The destination file already exists.


sourceFileName was not found.


sourceFileName or destFileName is null.


sourceFileName or destFileName is a zero-length string, contains only white space, or contains invalid characters as defined in InvalidPathChars.


The caller does not have the required permission.


The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.


The path specified in sourceFileName or destFileName is invalid, (for example, it is on an unmapped drive).


sourceFileName or destFileName is in an invalid format.

This method works across disk volumes, and it does not throw an exception if the source and destination are the same. Note that if you attempt to replace a file by moving a file of the same name into that directory, you get an IOException. You cannot use the Move method to overwrite an existing file.

The sourceFileName and destFileName arguments are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

If you try to move a file across disk volumes and that file is in use, the file is copied to the destination but not deleted from the source.

For a list of common I/O tasks, see Common I/O Tasks.

The following example moves a file.

using System;
using System.IO;

class Test 
    public static void Main() 
        string path = @"c:\temp\MyTest.txt";
        string path2 = @"c:\temp2\MyTest.txt";
            if (!File.Exists(path)) 
                // This statement ensures that the file is created,
                // but the handle is not kept.
                using (FileStream fs = File.Create(path)) {}

            // Ensure that the target does not exist.
            if (File.Exists(path2))	

            // Move the file.
            File.Move(path, path2);
            Console.WriteLine("{0} was moved to {1}.", path, path2);

            // See if the original exists now.
            if (File.Exists(path)) 
                Console.WriteLine("The original file still exists, which is unexpected.");
                Console.WriteLine("The original file no longer exists, which is expected.");

        catch (Exception e) 
            Console.WriteLine("The process failed: {0}", e.ToString());

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
© 2018 Microsoft