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");
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.
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.
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 theFile
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With