File.Move
Method
Definition
Moves a specified file to a new location, providing the option to specify a new file name.
public static void Move (string sourceFileName, string destFileName);
- sourceFileName
- String
The name of the file to move. Can include a relative or absolute path.
- destFileName
- String
The new path and name for the file.
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.
Examples
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";
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 )
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." );
}
else
{
Console::WriteLine( "The original file no longer exists, which is expected." );
}
}
catch ( Exception^ e )
{
Console::WriteLine( "The process failed: {0}", e );
}
}
using System;
using System.IO;
class Test
{
public static void Main()
{
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.
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());
}
}
}
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 = "c:\temp2\MyTest.txt"
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
Remarks
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 can include 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 it is not deleted from the source.
For a list of common I/O tasks, see Common I/O Tasks.