记录复制到CopyFileEx中的字节 [英] Recording bytes copied in CopyFileEx

查看:65
本文介绍了记录复制到CopyFileEx中的字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个测试工具,该工具测试来自kernel32.dll的CopyFileEx,以便在WAN上复制文件.

我的代码基于此处的链接

我要做的是使用netapi在C:\上创建一个共享文件夹,并将一个测试文件放在该文件夹内.然后,我创建了一个线程来调用CopyFileEx,并使用控制台应用程序中的主线程,在1秒钟后断开了共享文件夹的连接.这样做的目的是在重新连接共享文件夹并继续复制后,在CopyFileEx中调用COPY_FILE_RESTARTABLE时测试我的代码.

问题是,当我将流写入器添加到委托(CopyProgressHandler)中以将字节写入文本文件时,它引发了一个异常,即该文件正在被另一个进程使用.

因此,我的问题是,当我在自己的线程下运行CopyFileEx时,如何记录将CopyFileEx调用到文本文件时写入的字节数?

本质上,我需要记录删除并重新引入共享后写入的字节数.

I am trying to create a test harness, which tests CopyFileEx from kernel32.dll for copying files across the WAN.

My code is based upon on a link found on here

What I have done is create a shared folder on my C:\ using netapi and placed inside the folder a test file. Then I created a thread to call my CopyFileEx and using the main thread in my Console Application I disconnect my share folder after 1 second. The purpose of this is test my code when COPY_FILE_RESTARTABLE is called in CopyFileEx once the share folder is reconnected and the copying continues.

The problem is when I add to the delegate (CopyProgressHandler), a streamwriter to write the bytes to a text file, it throws an exception that the file is being used by another process.

So, my question is how can record the number of bytes that have been written when CopyFileEx has been called to a text file when I have it running under its own thread?

In essence I need to record the number of bytes written when a share is removed and reintroduced. Also is it possible to record the point when copying has been interrupted?

推荐答案

好,不要使用CopyFileEx.如果将文件复制为.NET代码,则可以对过程进行各种控制.您可以使用类System.IO.BinaryReaderSystem.IO.BinaryWriter并使用某个大小的缓冲区从一个文件读取数据并将其循环写入另一文件.参见:

http://msdn.microsoft.com/en-us/library/system.io. binaryreader.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.io. binarywriter.aspx [ ^ ].

& mash; SA
Well, don''t use CopyFileEx. You can get all kind of control over the process if you copy the file in .NET code. You can use the classes System.IO.BinaryReader and System.IO.BinaryWriter and read data from one file and write it in other file in cycle using a buffer of some size. See:

http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx[^].

&mash;SA


这篇关于记录复制到CopyFileEx中的字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆