File.Move Method (System.IO) (2024)

Edit

Share via

  • Reference

Definition

Namespace:
System.IO
Assemblies:
mscorlib.dll, System.IO.FileSystem.dll
Assembly:
System.IO.FileSystem.dll
Assembly:
System.Runtime.dll
Assembly:
mscorlib.dll
Assembly:
netstandard.dll

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Overloads

Move(String, String)

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

Move(String, String, Boolean)

Moves a specified file to a new location, providing the options to specify a new file name and to replace the destination file if it already exists.

Move(String, String)

Source:
File.cs
Source:
File.cs
Source:
File.cs

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

public: static void Move(System::String ^ sourceFileName, System::String ^ destFileName);
public static void Move (string sourceFileName, string destFileName);
static member Move : string * string -> unit
Public Shared Sub Move (sourceFileName As String, destFileName As String)

Parameters

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.

Exceptions

IOException

destFileName already exists.

-or-

An I/O error has occurred, e.g. while copying the file across disk volumes.

ArgumentNullException

sourceFileName or destFileName is null.

ArgumentException

.NET Framework and .NET Core versions older than 2.1: sourceFileName or destFileName is a zero-length string, contains only white space, or contains invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method.

UnauthorizedAccessException

The caller does not have the required permission.

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length.

DirectoryNotFoundException

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

NotSupportedException

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()); } }}
open System.IOlet path = @"c:\temp\MyTest.txt"let path2 = @"c:\temp2\MyTest.txt"if File.Exists path |> not then // This statement ensures that the file is created, // but the handle is not kept. use _ = File.Create path ()// Ensure that the target does not exist.if File.Exists path2 then File.Delete path2// Move the file.File.Move(path, path2)printfn $"{path} was moved to {path2}."// See if the original exists now.if File.Exists path then printfn "The original file still exists, which is unexpected."else printfn "The original file no longer exists, which is expected."
Imports System.IOImports System.TextPublic 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 SubEnd 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, an IOException is thrown. To avoid this problem:

  • In .NET Core 3.0 and later versions, you can call Move(String, String, Boolean) setting the parameter overwrite to true, which will replace the file if it exists.

  • In all .NET versions, you can call Copy(String, String, Boolean) to copy with overwrite, then call Delete to remove the excess source file. This strategy is advisable if the file being copied is small, and you are looking for an "atomic" file operation. If you Delete the file first, and the system or program crashes, the destination file will no longer exist.

  • In all .NET versions, you can call Delete(String) before calling Move, which will only delete the file if it exists.

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.

Moving the file across disk volumes is equivalent to copying the file and deleting it from the source if the copying was successful.

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.

See also

  • File and Stream I/O
  • Reading Text From A File
  • How to: Write Text to a File

Applies to

Move(String, String, Boolean)

Source:
File.cs
Source:
File.cs
Source:
File.cs

Moves a specified file to a new location, providing the options to specify a new file name and to replace the destination file if it already exists.

public: static void Move(System::String ^ sourceFileName, System::String ^ destFileName, bool overwrite);
public static void Move (string sourceFileName, string destFileName, bool overwrite);
static member Move : string * string * bool -> unit
Public Shared Sub Move (sourceFileName As String, destFileName As String, overwrite As Boolean)

Parameters

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.

overwrite
Boolean

true to replace the destination file if it already exists; false otherwise.

Exceptions

IOException

destFileName already exists and overwrite is false.

-or-

An I/O error has occurred, e.g. while copying the file across disk volumes.

FileNotFoundException

sourceFileName was not found.

ArgumentNullException

sourceFileName or destFileName is null.

ArgumentException

.NET Framework and .NET Core versions older than 2.1: sourceFileName or destFileName is a zero-length string, contains only white space, or contains invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method.

UnauthorizedAccessException

The caller does not have the required permission.

-or-

The Operating System has failed to acquire an exclusive access to the destination file.

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length.

DirectoryNotFoundException

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

NotSupportedException

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()); } }}
open System.IOlet path = @"c:\temp\MyTest.txt"let path2 = @"c:\temp2\MyTest.txt"if File.Exists path |> not then // This statement ensures that the file is created, // but the handle is not kept. use _ = File.Create path ()// Ensure that the target does not exist.if File.Exists path2 then File.Delete path2// Move the file.File.Move(path, path2)printfn $"{path} was moved to {path2}."// See if the original exists now.if File.Exists path then printfn "The original file still exists, which is unexpected."else printfn "The original file no longer exists, which is expected."
Imports System.IOImports System.TextPublic 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 SubEnd Class

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 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.

Moving the file across disk volumes is equivalent to copying the file and deleting it from the source if the copying was successful.

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.

See also

  • File and Stream I/O
  • Reading Text From A File
  • How to: Write Text to a File

Applies to

File.Move Method (System.IO) (2024)
Top Articles
How do you guarantee a personal loan
What is a WAN IP Address & How To Find It?
Craigslist Myrtle Beach Motorcycles For Sale By Owner
Fiskars X27 Kloofbijl - 92 cm | bol
Netr Aerial Viewer
Nybe Business Id
Bin Stores in Wisconsin
Air Canada bullish about its prospects as recovery gains steam
Retro Ride Teardrop
More Apt To Complain Crossword
Employeeres Ual
Premier Boating Center Conroe
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
How To Delete Bravodate Account
Athens Bucket List: 20 Best Things to Do in Athens, Greece
“In my day, you were butch or you were femme”
Cinebarre Drink Menu
Paradise leaked: An analysis of offshore data leaks
Boston Gang Map
Ibukunore
Daylight Matt And Kim Lyrics
Walmart Car Department Phone Number
Promiseb Discontinued
Robeson County Mugshots 2022
What Channel Is Court Tv On Verizon Fios
Kcwi Tv Schedule
A Person That Creates Movie Basis Figgerits
Blackboard Login Pjc
Masterbuilt Gravity Fan Not Working
Aladtec Login Denver Health
Phone number detective
Watchdocumentaries Gun Mayhem 2
2012 Street Glide Blue Book Value
Personalised Handmade 50th, 60th, 70th, 80th Birthday Card, Sister, Mum, Friend | eBay
Grapes And Hops Festival Jamestown Ny
KITCHENAID Tilt-Head Stand Mixer Set 4.8L (Blue) + Balmuda The Pot (White) 5KSM175PSEIC | 31.33% Off | Central Online
Scarlet Maiden F95Zone
2132815089
Below Five Store Near Me
Home Auctions - Real Estate Auctions
Skyward Cahokia
Ratchet And Clank Tools Of Destruction Rpcs3 Freeze
Value Village Silver Spring Photos
Mejores páginas para ver deportes gratis y online - VidaBytes
Craigslist Pet Phoenix
Used Auto Parts in Houston 77013 | LKQ Pick Your Part
Cvs Minute Clinic Women's Services
Fredatmcd.read.inkling.com
The Goshen News Obituary
Gelato 47 Allbud
Coldestuknow
Guidance | GreenStar™ 3 2630 Display
Latest Posts
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 6316

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.