gpt4 book ai didi

c# - CopyFileEx 包装器参数

转载 作者:太空狗 更新时间:2023-10-29 21:23:23 27 4
gpt4 key购买 nike

我最近不得不从使用 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, destination, options, null);
}

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

public static void CopyFile(FileInfo source, FileInfo destination,
CopyFileOptions options, 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, destination, callback, 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, FileInfo destination,
CopyFileCallback callback, 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, long streamSize,
long streamBytesTransferred, int streamNumber, int callbackReason,
IntPtr sourceFile, IntPtr destinationFile, 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, FileInfo destination, 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, FileInfo destination, object state, long totalFileSize, 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);

关于c# - CopyFileEx 包装器参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13976253/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com