【问题标题】:Most efficient way to rename an image重命名图像的最有效方法
【发布时间】:2014-09-03 22:31:46
【问题描述】:

在 Windows 中是否有比以下更有效的重命名文件的方法:

System.IO.File.Move(sourceFileName, destFileName);  

我有数百万个,使用上述方法需要将近一周的时间才能运行。

【问题讨论】:

  • 文件重命名有什么规律吗?
  • “我有数百万个,使用上面的代码将需要将近一周的时间来运行” - 这意味着一次重命名大约需要。 0.0672 秒(基于 1 周和 900 万个文件)
  • @Raptor 是的,它会根据文本文件中的标准而变化。程序读取文件中的一行,查看文本并根据行中的内容更改名称。
  • @MitchWheat 你的意思是什么?我有更多。

标签: c# image rename file-move


【解决方案1】:

File.Move 是 Win32 API 的精简包装器。我怀疑除了直接在磁盘上的块级别修改原始数据之外还有什么更快的方法。我认为从托管代码中寻找更快的方法是不走运的。

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

【讨论】:

    猜你喜欢
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2013-12-08
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 2018-04-10
    相关资源
    最近更新 更多