【问题标题】:FileInfo.MoveTo() vs File.Move()FileInfo.MoveTo() 与 File.Move()
【发布时间】:2011-02-13 13:36:17
【问题描述】:

这两种移动文件的方法有什么区别吗?

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");

【问题讨论】:

  • 问题是什么?它们似乎是达到相同 API 的两种不同方法。您是在寻找循环性能或最佳实践还是???
  • 我只是很好奇为什么有两种方法看起来完全一样。
  • 希望下面的 sn-ps 向您展示它们的不同之处?此外,如果您没有使用 Red Gate 的(当前免费)Reflector 产品,您应该使用。
  • 感谢您的帮助。是的,我有反射器。我只是在尝试对其他 3rd 方代码进行逆向工程时才考虑在实际框架上使用它。

标签: c# .net file-io


【解决方案1】:

查看此 MSDN 页面http://msdn.microsoft.com/en-us/library/akth6b1k.aspx 中的“备注”部分:

如果您要多次重用一个对象,请考虑使用FileInfo 的实例方法而不是File 类的相应静态方法,因为并不总是需要进行安全检查。

我认为这种差异在 File (Directory) 和 FileInfo (DirectoryInfo) 类之间最为显着。

更新:类似问题中的相同解释:https://stackoverflow.com/a/1324808/380123

【讨论】:

    【解决方案2】:

    通过 RedGate 反射器:

    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;
    }
    

    【讨论】:

      【解决方案3】:

      一个重要的区别是FileInfo.MoveTo() 会将FileInfo 对象的文件路径 更新为目标路径。 File.Move() 显然不是这种情况,因为它只使用字符串作为输入。

      【讨论】:

        【解决方案4】:

        我能看到的唯一显着区别是 File.Move 是静态的,而 FileInfo.MoveTo 不是。
        除此之外,它们运行的​​代码大致相同。

        【讨论】:

        • 是的,我同意; FileInfo 继承 FileSystemInfo 而 File 只是继承 Object。 FileSystemInfo 显然只提供编组,所以我猜 FileInfo 更易于管理。
        猜你喜欢
        • 2016-01-11
        • 1970-01-01
        • 2011-10-25
        • 1970-01-01
        • 1970-01-01
        • 2014-06-25
        • 1970-01-01
        • 2011-10-16
        • 1970-01-01
        相关资源
        最近更新 更多