首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >FileInfo.MoveTo() vs File.Move()

FileInfo.MoveTo() vs File.Move()
EN

Stack Overflow用户
提问于 2010-04-29 05:32:44
回答 2查看 36.5K关注 0票数 24

这两种移动文件的方法有什么不同吗?

代码语言:javascript
复制
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");
EN

回答 2

Stack Overflow用户

发布于 2010-04-29 05:39:13

通过RedGate反射器:

File.Move()

代码语言:javascript
复制
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()

代码语言:javascript
复制
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;
}
票数 14
EN

Stack Overflow用户

发布于 2010-04-29 05:40:25

我能看到的唯一显著区别是File.Move是静态的,而FileInfo.MoveTo不是。

除此之外,它们运行的代码大致相同。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2733288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档