c# – CopyFileEx包装器参数

前端之家收集整理的这篇文章主要介绍了c# – CopyFileEx包装器参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我最近不得不从使用File.Copy()更改为CopyFileEx,我正在努力寻找如何使用它.

经过大量的谷歌搜索,我找到了this好的包装器来使用它,但我需要的是获取当前文件的复制字节的进度,并且如果可能的话计算复制我传递给它的所有文件的进度.

(我知道有些项目的进度条与CopyFileEx相关联,但我没有足够的经验来提取相关代码,我想使用这个包装器).

大概只是将它与要复制的文件的总字节数进行比较,我会事先找到它,并从中计算出百分比.

我目前的复制方法

FileRoutines.CopyFile(new FileInfo("source.txt"),new FileInfo("dest.txt"));

我坚持的是如何使用获取进度信息所需的参数来重载它.

public sealed class FileRoutines
{
    public static void CopyFile(FileInfo source,FileInfo destination)
    {
        CopyFile(source,destination,CopyFileOptions.None);
    }

    public static void CopyFile(FileInfo source,FileInfo destination,CopyFileOptions options)
    {
        CopyFile(source,options,null);
    }

    public static void CopyFile(FileInfo source,CopyFileOptions options,CopyFileCallback callback)
    {
        CopyFile(source,callback,CopyFileCallback callback,object state)
    {
        if (source == null) throw new ArgumentNullException("source");
        if (destination == null) 
            throw new ArgumentNullException("destination");
        if ((options & ~CopyFileOptions.All) != 0) 
            throw new ArgumentOutOfRangeException("options");

        new FileIOPermission(
            FileIOPermissionAccess.Read,source.FullName).Demand();
        new FileIOPermission(
            FileIOPermissionAccess.Write,destination.FullName).Demand();

        CopyProgressRoutine cpr = callback == null ? 
            null : new CopyProgressRoutine(new CopyProgressData(
                source,state).CallbackHandler);

        bool cancel = false;
        if (!CopyFileEx(source.FullName,destination.FullName,cpr,IntPtr.Zero,ref cancel,(int)options))
        {
            throw new IOException(new Win32Exception().Message);
        }
    }

    private class CopyProgressData
    {
        private FileInfo _source = null;
        private FileInfo _destination = null;
        private CopyFileCallback _callback = null;
        private object _state = null;

        public CopyProgressData(FileInfo source,object state)
        {
            _source = source; 
            _destination = destination;
            _callback = callback;
            _state = state;
        }

        public int CallbackHandler(
            long totalFileSize,long totalBytesTransferred,long streamSize,long streamBytesTransferred,int streamNumber,int callbackReason,IntPtr sourceFile,IntPtr destinationFile,IntPtr data)
        {
            return (int)_callback(_source,_destination,_state,totalFileSize,totalBytesTransferred);
        }
    }

    private delegate int CopyProgressRoutine(
        long totalFileSize,long TotalBytesTransferred,IntPtr data);

    [SuppressUnmanagedCodeSecurity]
    [DllImport("Kernel32.dll",CharSet=CharSet.Auto,SetLastError=true)]
    private static extern bool CopyFileEx(
        string lpExistingFileName,string lpNewFileName,CopyProgressRoutine lpProgressRoutine,IntPtr lpData,ref bool pbCancel,int dwCopyFlags);
}

public delegate CopyFileCallbackAction CopyFileCallback(
    FileInfo source,object state,long totalFileSize,long totalBytesTransferred);

public enum CopyFileCallbackAction
{
    Continue = 0,Cancel = 1,Stop = 2,Quiet = 3
}

[Flags]
public enum CopyFileOptions
{
    None = 0x0,FailIfDestinationExists = 0x1,Restartable = 0x2,AllowDecryptedDestination = 0x8,All = FailIfDestinationExists | Restartable | AllowDecryptedDestination
}

任何指针真的很感激.

解决方法

包装器已经具有处理进度所需的管道.只需在返回之前实现代码以在CallbackHandler中更新进度条. progressBar1.Maximum默认为100,因此下面的代码将计算百分比.

用以下内容替换当前的CopyFile调用

CopyFileCallbackAction myCallback(FileInfo source,long totalBytesTransferred)
{
    double dProgress = (totalBytesTransferred / (double)totalFileSize) * 100.0;
    progressBar1.Value = (int)dProgress;
    return CopyFileCallbackAction.Continue;
}

FileRoutines.CopyFile(new FileInfo("source.txt"),new FileInfo("dest.txt"),myCallback);
原文链接:https://www.f2er.com/csharp/243075.html

猜你在找的C#相关文章