Export (0) Print
Expand All

File.Move Method

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

[Visual Basic]
Public Shared Sub Move( _
   ByVal sourceFileName As String, _
   ByVal destFileName As String _
)
[C#]
public static void Move(
 string sourceFileName,
 string destFileName
);
[C++]
public: static void Move(
 String* sourceFileName,
 String* destFileName
);
[JScript]
public static function Move(
   sourceFileName : String,
 destFileName : String
);

Parameters

sourceFileName
The name of the file to move.
destFileName
The new path for the file.

Exceptions

Exception Type Condition
IOException The destination file already exists.
ArgumentNullException sourceFileName or destFileName is a null reference (Nothing in Visual Basic).
ArgumentException sourceFileName or destFileName is a zero-length string, contains only white space, or contains invalid characters as defined in InvalidPathChars.
UnauthorizedAccessException The caller does not have the required permission.
FileNotFoundException sourceFileName was not found.
PathTooLongException 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.
DirectoryNotFoundException The path specified in sourceFileName or destFileName is invalid, such as being on an unmapped drive.
NotSupportedException sourceFileName or destFileName is in an invalid format.

Remarks

This method works across disk volumes, and it does not throw an exception if the source and destination are the same.

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 below. 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. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File

File.AppendText

FileInfo.AppendText

Copy a file. File.Copy

FileInfo.CopyTo

Rename or move a directory. Directory.Move

DirectoryInfo.MoveTo

Example

[Visual Basic, C#, C++] The following example moves a file.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        Dim path2 As String = path + "temp"

        Try
            If File.Exists(path) = False Then
                ' This statement ensures that the file is created,
                ' but the handle is not kept.
                Dim fs As FileStream = File.Create(path)
                fs.Close()
            End If

            ' Ensure that the target does not exist.
            If File.Exists(path2) Then
                File.Delete(path2)
            End If

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

            ' See if the original file exists now.
            If File.Exists(path) Then
                Console.WriteLine("The original file still exists, which is unexpected.")
            Else
                Console.WriteLine("The original file no longer exists, which is expected.")
            End If
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

[C#] 
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
        string path2 = path + "temp";
        try 
        {
            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))    
            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.");
            } 
            else 
            {
                Console.WriteLine("The original file no longer exists, which is expected.");
            }            

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

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    String* path = S"c:\\temp\\MyTest.txt";
    String* path2 = String::Concat(path, S"temp");
    try {
        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) __try_cast<IDisposable*>(fs)->Dispose();
        }

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

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

        // See if the original exists now.
        if (File::Exists(path)) {
            Console::WriteLine(S"The original file still exists, which is unexpected.");
        } else {
            Console::WriteLine(S"The original file no longer exists, which is expected.");
        }            
    } catch (Exception* e) {
        Console::WriteLine(S"The process failed: {0}", e);
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

File Class | File Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft