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

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

问题描述

我创建了一个C#中的复制实用程序(.NET 2.0框架)的复制文件,目录和递归子目录等程序有一个图形用户界面,显示当前文件被复制,当前文件编号(序列),要复制的文件的总数量和百分比完成了复制操作。还有一个进度条,这是基于当前文件/文件总数。

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.

我的问题是有关复制大文件。我一直无法找到一种方式来表示一个大文件的总拷贝的进展(使用我当前的阶级结构utilitzes FileInfo.CopyTo法)。作为一种变通方法我已经分开了文件复制操作和图形用户界面显示自己的线程,并成立了一个视觉提示,显示正在做这项工作。至少用户意识到该程序不凝固并且还在复制文件。

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包装几分钟,像它描述如下: <一href="http://www.pinvoke.net/default.aspx/kernel32.CopyFileEx">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天全站免登陆