24

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");
Eric Anastas
  • 21,675
  • 38
  • 142
  • 236
  • 1
    What is the question? They appear to be two different methods to reach the same API. Are you looking for looped performance or best practices or ??? – jcolebrand Apr 28 '10 at 21:34
  • 1
    I was just curious why there were two methods that appear to do exactly the same thing. – Eric Anastas Apr 29 '10 at 01:22
  • Well hopefully the snippets below show you how they differ? Also, if you're not using the (currently free) Reflector product from Red Gate, you should. – jcolebrand Apr 29 '10 at 14:32
  • Thanks that helps a lot. And yes I have Reflector. I just never think to use it on the actual framework only when I'm trying to reverse engineer some other 3rd party code. – Eric Anastas Apr 29 '10 at 18:07

4 Answers4

25

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

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
MiSHuTka
  • 1,191
  • 1
  • 12
  • 20
14

Via RedGate Reflector:

File.Move()

public static void Move(string sourceFileName, string destFileName)
{
    if ((sourceFileName == null) || (destFileName == null))
    {
        throw new ArgumentNullException((sourceFileName == null) ? "sourceFileName" : "destFileName", Environment.GetResourceString("ArgumentNull_FileName"));
    }
    if ((sourceFileName.Length == 0) || (destFileName.Length == 0))
    {
        throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), (sourceFileName.Length == 0) ? "sourceFileName" : "destFileName");
    }
    string fullPathInternal = Path.GetFullPathInternal(sourceFileName);
    new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, new string[] { fullPathInternal }, false, false).Demand();
    string dst = Path.GetFullPathInternal(destFileName);
    new FileIOPermission(FileIOPermissionAccess.Write, new string[] { dst }, false, false).Demand();
    if (!InternalExists(fullPathInternal))
    {
        __Error.WinIOError(2, fullPathInternal);
    }
    if (!Win32Native.MoveFile(fullPathInternal, dst))
    {
        __Error.WinIOError();
    }
}

and FileInfo.MoveTo()

public void MoveTo(string destFileName)
{
    if (destFileName == null)
    {
        throw new ArgumentNullException("destFileName");
    }
    if (destFileName.Length == 0)
    {
        throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName");
    }
    new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, new string[] { base.FullPath }, false, false).Demand();
    string fullPathInternal = Path.GetFullPathInternal(destFileName);
    new FileIOPermission(FileIOPermissionAccess.Write, new string[] { fullPathInternal }, false, false).Demand();
    if (!Win32Native.MoveFile(base.FullPath, fullPathInternal))
    {
        __Error.WinIOError();
    }
    base.FullPath = fullPathInternal;
    base.OriginalPath = destFileName;
    this._name = Path.GetFileName(fullPathInternal);
    base._dataInitialised = -1;
}
jcolebrand
  • 15,889
  • 12
  • 75
  • 121
13

An important difference is that FileInfo.MoveTo() will update the filepath of the FileInfo object to the destination path. This is obviously not the case of File.Move() since it only uses strings as input.

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
Robert
  • 2,357
  • 4
  • 25
  • 46
2

The only significant difference I can see is File.Move is static and FileInfo.MoveTo is not.
Apart from that they run approximately the same code.

Jens Granlund
  • 4,950
  • 1
  • 31
  • 31
  • Yeah I agree; and FileInfo inherits FileSystemInfo versus File just inherits Object. FileSystemInfo apparently only gives Marshalling, so I would guess that FileInfo is more Managed Friendly. – jcolebrand Apr 28 '10 at 21:47