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

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

问题描述

我在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#.

此方法不支持进度回调。

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

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

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