我可以在 .NET 中使用 FileInfo.CopyTo() 显示文件复制进度吗? [英] Can I show file copy progress using FileInfo.CopyTo() in .NET?

查看:17
本文介绍了我可以在 .NET 中使用 FileInfo.CopyTo() 显示文件复制进度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 c# (.NET 2.0 Framework) 中创建了一个复制实用程序,用于复制文件、目录和递归子目录等.该程序有一个 GUI,显示正在复制的当前文件、当前文件编号(序列)、要复制的文件总数和复制操作完成的百分比.还有一个基于当前文件/总文件的进度条.

I've created a copy utility in c# (.NET 2.0 Framework) that copies files, directories and recursive sub directories etc. The program has a GUI that shows the current file being copied, the current file number (sequence), the total number of files to be copied and the percentage completed for the copy operations. There is also a progress bar, that is based on current file / total files.

我的问题与复制大文件有关.我一直无法找到一种方法来指示大文件的总复制进度(使用我当前使用 FileInfo.CopyTo 方法的类结构).作为一种解决方法,我将文件复制操作和 GUI 显示分离到它们自己的线程中,并设置了一个视觉提示来显示工作正在完成.至少用户知道程序没有被冻结并且仍在复制文件.

My problem is related to copying large files. I've been unable to find a way to indicate the total copy progress of a large file (using my current class structure that utilitzes FileInfo.CopyTo method). As a workaround I've separated the file copy operations and GUI display to their own threads and set up a visual cue to show that work is being done. At least the user is aware that the program isn't frozen and is still copying files.

如果能够根据总字节数显示进度,或者从 FileInfo.CopyTo 方法触发某种类型的事件,指示从当前文件复制的总字节数,那就更好了.

It would be nicer to be able to show the progress based on the total number of bytes or have some type of event that fires from the FileInfo.CopyTo method that indicates the total number of bytes copied from the current file.

我知道 FileInfo.Length 属性,所以我确信有一种方法 MacGuyver 我自己的事件基于此,并且在读取更新的事物的 GUI 端有一个处理程序(可能基于检查使用某种类型的计时器的目标对象的 FileInfo.Length 属性?).

I'm aware of the FileInfo.Length property, so I'm sure there is a way MacGuyver my own event that is based on this and have a handler on the GUI side of things reading the updates (maybe based on checking the FileInfo.Length property of the destination object using some type of timer?).

有没有人知道我忽略的一种方法.如果我可以避免它,我宁愿不重写我的类以通过流复制字节并以这种方式跟踪它(尽管我认为我可能会坚持走这条路).

Does anyone know of a way to do this that I'm overlooking. If I can avoid it, I'd rather not rewrite my class to copy bytes through a stream and track it that way (though I'm thinking I might be stuck with going that route).

PS - 我现在坚持使用 .NET 2.0 框架,因此任何只需要 >= 3.0 中可用功能的解决方案都不适合我.

PS - I'm stuck with the .NET 2.0 framework for now, so any solution that requires features available in >= 3.0 only are not an option for me.

PPS - 我对任何 .NET 语言种类的解决方案持开放态度,而不仅仅是 c#.

PPS - I'm open to solutions in any .NET language variety, not only c#.

推荐答案

FileInfo.CopyTo 基本上是 Win32 API 调用CopyFile"的包装器.在 kernel32.dll 中.该方法不支持进度回调.

The FileInfo.CopyTo is basically a wrapper around the Win32 API call "CopyFile" in the kernel32.dll. This method does not support progress callback.

但是,CopyFileEx 方法可以,您可以在几分钟内围绕它编写自己的 .NET 包装器,如下所述:http://www.pinvoke.net/default.aspx/kernel32.CopyFileEx

However, the CopyFileEx method does, and you can write your own .NET wrapper around it in a few minutes, like it is described here: http://www.pinvoke.net/default.aspx/kernel32.CopyFileEx

这篇关于我可以在 .NET 中使用 FileInfo.CopyTo() 显示文件复制进度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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