简体   繁体   English

重命名图像的最有效方法

[英]Most efficient way to rename an image

Is there a more efficient way to rename a file in Windows other than: 除了以下方法以外,是否有更有效的方法来重命名Windows中的文件:

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

I have millions of them and using the above will take almost a week to run. 我有数百万个,使用上面的代码将花费几乎一周的时间。

File.Move is a thin wrapper around the Win32 API. File.Move是Win32 API的瘦包装。 I doubt there is any faster way short of directly modifying the raw data at the block level on the disk. 我怀疑除了在磁盘的块级别直接修改原始数据之外,还有没有其他更快的方法。 I think you are out of luck looking for a faster way from managed code. 我认为您不希望从托管代码中找到更快的方法。

File.Move decompiled source: 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM