Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileInfo.MoveTo() vs File.Move()

Tags:

Is there any difference between these two methods of moving a file?

System.IO.FileInfo f = new System.IO.FileInfo(@"c:\foo.txt"); f.MoveTo(@"c:\bar.txt");  //vs  System.IO.File.Move(@"c:\foo.txt", @"c:\bar.txt"); 
like image 778
Eric Anastas Avatar asked Apr 28 '10 21:04

Eric Anastas


People also ask

Does file move delete?

Moving – move the original files or folder from one place to another (change the destination). The move deletes the original file or folder, while copy creates a duplicate.

Does file move overwrite?

File. Move() doesn't support overwriting of an existing file. In fact, it will throw an IOException if a file with the same path as sourceDestFilename already exists.


1 Answers

Take a look at "Remarks" section in this MSDN page http://msdn.microsoft.com/en-us/library/akth6b1k.aspx :

If you are going to reuse an object several times, consider using the instance method of FileInfo instead of the corresponding static methods of the File class, because a security check will not always be necessary.

I think this difference is most significant between File (Directory) and FileInfo (DirectoryInfo) classes.

Update: The same explanation in similar question: https://stackoverflow.com/a/1324808/380123

like image 166
MiSHuTka Avatar answered Sep 18 '22 10:09

MiSHuTka