Export (0) Print
Expand All

FileInfo.MoveTo Method

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

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

Parameters

destFileName
The path to move the file to, which can specify a different file name.

Exceptions

Exception Type Condition
IOException An I/O error occurs, such as the destination file already exists or the destination device is not ready.
ArgumentNullException destFileName is a null reference (Nothing in Visual Basic).
ArgumentException destFileName is empty, contains only white spaces, or contains invalid characters.
SecurityException The caller does not have the required permission.
UnauthorizedAccessException destFileName is read-only or is a directory.
FileNotFoundException The file is not found.
DirectoryNotFoundException The specified path is invalid, such as being on an unmapped drive.
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.
NotSupportedException destFileName contains a colon (:) in the middle of the string.

Remarks

This method works across disk volumes. For example, the file c:\MyFile.txt can be moved to d:\public and renamed NewFile.txt.

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

Rename or move a directory. Directory.Move

DirectoryInfo.MoveTo

.NET Compact Framework Platform Note:  Some device file systems do not support relative paths. Specify absolute path information.

Example

The following example demonstrates moving a file to a different location and renaming the file.

[Visual Basic] 
Imports System
Imports System.IO

Public Class MoveToTest
    Public Shared Sub Main()
        ' Create a reference to a file, which might or might not exist.
        ' If it does not exist, it is not yet created.
        Dim fi As New FileInfo("temp.txt")
        ' Create a writer, ready to add entries to the file.
        Dim sw As StreamWriter = fi.AppendText()
        sw.WriteLine("Add as many lines as you like...")
        sw.WriteLine("Add another line to the output...")
        sw.Flush()
        sw.Close()
        If File.Exists("newTemp.txt") Then
            ' Ensure that the target file does not exist, since this is disallowed.
            File.Delete("newTemp.txt")
        End If
        ' Move this file to another file.
        fi.MoveTo("newTemp.txt")
        Dim sr As New StreamReader(fi.OpenRead())
        ' Get the information out of the new file, and display it.
        Console.WriteLine("{0}This is the information in the new file '{1}':", Environment.NewLine, fi.Name)
        While sr.Peek() <> -1
            Console.WriteLine(sr.ReadLine())
        End While
    End Sub 'Main
End Class 'MoveToTest

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

public class MoveToTest 
{
    public static void Main() 
    {
        // Create a reference to a file, which might or might not exist.
        // If it does not exist, it is not yet created.
        FileInfo fi = new FileInfo("temp.txt");
        // Create a writer, ready to add entries to the file.
        StreamWriter sw = fi.AppendText();
        sw.WriteLine("Add as many lines as you like...");
        sw.WriteLine("Add another line to the output...");
        sw.Flush();
        sw.Close();
        // Ensure that the target file does not exist, since this is disallowed.
        if (File.Exists("newTemp.txt"))
            File.Delete("newTemp.txt");
        // Move this file to another file.
        fi.MoveTo("newTemp.txt");
        // Get the information out of the new file and display it.
        StreamReader sr = new StreamReader( fi.OpenRead() );
        Console.WriteLine("{0}This is the information in the new file '{1}':", Environment.NewLine, fi.Name);
        while (sr.Peek() != -1)
            Console.WriteLine( sr.ReadLine() );
    }
}

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

using namespace System;
using namespace System::IO;

int main() {
    // Create a reference to a file, which might or might not exist.
    // If it does not exist, it is not yet created.
    FileInfo* fi = new FileInfo(S"temp.txt");
    // Create a writer, ready to add entries to the file.
    StreamWriter* sw = fi->AppendText();
    sw->WriteLine(S"Add as many lines as you like...");
    sw->WriteLine(S"Add another line to the output...");
    sw->Flush();
    sw->Close();
    // Ensure that the target file does not exist, since this is disallowed.
    if (File::Exists(S"newTemp.txt"))
        File::Delete(S"newTemp.txt");
    // Move this file to another file.
    fi->MoveTo(S"newTemp.txt");
    // Get the information out of the new file and display it.
    StreamReader* sr = new StreamReader(fi->OpenRead());
    Console::WriteLine(S"{0}This is the information in the new file '{1}':", Environment::NewLine, fi->Name);
    while (sr->Peek() != -1)
        Console::WriteLine(sr->ReadLine());
}

[JScript] 
import System;
import System.IO;

public class MoveToTest {
    public static function Main() : void {

        // Create a reference to a file, which might or might not exist.
        // If it does not exist, it is not yet created.
        var fi : FileInfo = new FileInfo("temp.txt");
        // Create a writer, ready to add entries to the file.
        var sw : StreamWriter = fi.AppendText();
        sw.WriteLine("Add as many lines as you like...");
        sw.WriteLine("Add another line to the output...");
        sw.Flush();
        sw.Close();
        // Ensure that the target file does not exist, since this is disallowed.
        if (File.Exists("newTemp.txt"))
            File.Delete("newTemp.txt");
        // Move this file to another file.
        fi.MoveTo("newTemp.txt");
        // Get the information out of the new file and display it.
        var sr : StreamReader = new StreamReader( fi.OpenRead() );
        Console.WriteLine("{0}This is the information in the new file '{1}':", Environment.NewLine, fi.Name);
        while (sr.Peek() != -1)
            Console.WriteLine( sr.ReadLine() );
    }
}
MoveToTest.Main();

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

.NET Framework Security: 

See Also

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

Show:
© 2014 Microsoft