FileInfo. MoveTo () по сравнению с файлом. Переместитесь ()

Есть ли какое-либо различие между этими двумя методами перемещения файла?

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");
24
задан Eric Anastas 28 April 2010 в 21:32
поделиться

2 ответа

Единственное существенное различие, которое я вижу, это то, что File.Move статичен, а FileInfo.MoveTo - нет.
В остальном у них примерно одинаковый код.

2
ответ дан 28 November 2019 в 23:08
поделиться

Через 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();
    }
}

и 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;
}
13
ответ дан 28 November 2019 в 23:08
поделиться
Другие вопросы по тегам:

Похожие вопросы: