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)

static void Move (
	String^ sourceFileName, 
	String^ destFileName
public static void Move (
	String sourceFileName, 
	String destFileName
public static function Move (
	sourceFileName : String, 
	destFileName : String



The name of the file to move.


The new path for the file.

Exception typeCondition


The destination file already exists.


sourceFileName or destFileName is a null reference (Nothing in Visual Basic).


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.


sourceFileName was not found.


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.

For an example of using this method, see the Example section. The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Append text to a file.

How to: Open and Append to a Log File



Copy a file.



Rename or move a directory.



The following example moves a file.

using namespace System;
using namespace System::IO;

int 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.
         FileStream^ fs = File::Create( path );
         if ( fs )
                  delete (IDisposable^)fs;
      // Ensure that the target does not exist.
      if ( File::Exists( path2 ) )
            File::Delete( 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 );

import System.*;
import System.IO.*;

class Test
    public static void main(String[] args)
        String path = "c:\\temp\\MyTest.txt";
        String path2 = "c:\\temp2\\MyTest.txt";

        try {
            if (!(File.Exists(path))) {
                // This statement ensures that the file is created,
                // but the handle is not kept.
                FileStream fs = File.Create(path);

                try {
                finally {

            // 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.");
            else {
                Console.WriteLine("The original file no longer exists, " 
                    + "which is expected.");
        catch (System.Exception e) {
            Console.WriteLine("The process failed: {0}", e.ToString());
    } //main
} //Test

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0