| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 675 人关注过本帖
标题:如何计算复制文件的进度并显示
只看楼主 加入收藏
jiaguofu2005
Rank: 2
等 级:论坛游民
帖 子:47
专家分:59
注 册:2009-9-2
结帖率:92.31%
收藏
已结贴  问题点数:9 回复次数:3 
如何计算复制文件的进度并显示
如题,求高手指点一二
搜索更多相关主题的帖子: 进度 文件 
2010-01-21 20:21
skl315
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-1-9
收藏
得分:0 
新手  过来学习一下  
2010-01-21 21:18
qingzhu5753
Rank: 4
等 级:业余侠客
帖 子:64
专家分:248
注 册:2009-10-28
收藏
得分:0 
应该跟流有关吧,具体没弄过,学习!
2010-01-22 09:05
heshumeng
Rank: 2
等 级:论坛游民
帖 子:12
专家分:50
注 册:2009-9-2
收藏
得分:9 
*****************************************************************
** File Name:       frmMain.cs
** Copyright (c) 1999 -2003
** Creator:          FirePhoenix
** Created Date:     2004-11-13 15:24
** Modifier:
** Modify Date:
** Description:         
** Version:1.0
******************************************************************/

#region Using Directives
using System;
using ;
using System.Xml ;
using System.Collections ;
using System.Reflection ;
using System.Text ;
using System.Data ;
using
using System.Windows.Forms;
using System.Drawing;
using System.Threading ;
#endregion

namespace WindowsApplication4
{
/// <summary>
/// Copy Large File
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
   #region
   private System.Windows.Forms.ProgressBar progressBar1;
   private System.Windows.Forms.Button btnCopy;
   /// <summary>
   /// 必需的设计器变量。
   /// </summary>
   private components = null;

   public frmMain()
   {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();

    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
   }

   /// <summary>
   /// 清理所有正在使用的资源。
   /// </summary>
   protected override void Dispose( bool disposing )
   {
    if( disposing )
    {
     if (components != null)
     {
      components.Dispose();
     }
    }
    base.Dispose( disposing );
   }

   #region Initialize Components
   /// <summary>
   /// 设计器支持所需的方法 - 不要使用代码编辑器修改
   /// 此方法的内容。
   /// </summary>
   private void InitializeComponent()
   {
    this.progressBar1 = new System.Windows.Forms.ProgressBar();
    this.btnCopy = new System.Windows.Forms.Button();
    this.SuspendLayout();
    //
    // progressBar1
    //
    this.progressBar1.Location = new System.Drawing.Point(8, 16);
    this.progressBar1.Name = "progressBar1";
    this.progressBar1.Size = new System.Drawing.Size(208, 16);
    this.progressBar1.TabIndex = 0;
    //
    // btnCopy
    //
    this.btnCopy.Location = new System.Drawing.Point(8, 48);
    this.btnCopy.Name = "btnCopy";
    this.btnCopy.TabIndex = 1;
    this.btnCopy.Text = "Copy";
    this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click);
    //
    // frmMain
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(232, 77);
    this.Controls.Add(this.btnCopy);
    this.Controls.Add(this.progressBar1);
    this.Name = "frmMain";
    this.Text = "Copy File";
    this.ResumeLayout(false);

   }
   #endregion

   /// <summary>
   /// Entry Point
   /// </summary>
   [STAThread]
   static void Main()
   {
    Application.Run(new frmMain());
   }


   #endregion

   int totalSize; //Total Size
   int position;   //Position
   const int BUFFER_SIZE = 4096;
   byte[] buffer;
   Stream stream;

   private void btnCopy_Click(object sender, System.EventArgs e)
   {
    string strFile = "";

    OpenFileDialog dlg = new OpenFileDialog();
    if ( dlg.ShowDialog() == DialogResult.OK )
    {
     strFile = dlg.FileName ;
    }
    else
    {
     return ;
    }

    FileStream fs = new FileStream( strFile , FileMode.Open , FileAccess.Read ) ;
    totalSize = (int)fs.Length ;
    stream = fs;

    //Delete file which aready exists.
    if ( File.Exists( "c:\\copyedFile.bin" ) )
     File.Delete( "c:\\copyedFile.bin" );

    //Copy file while larger than 4KB.
    if ( totalSize > BUFFER_SIZE )
    {
     buffer = new byte[ BUFFER_SIZE ];

     // Async Invoke
     stream.BeginRead( buffer , 0 , BUFFER_SIZE , new AsyncCallback( AsyncCopyFile ) , null );   
    }
    else
    {
     fs.Close();
    }
   
   }
        
   /// <summary>
   /// Asynchronously copy file
   /// </summary>
   /// <param name="ar"></param>
   private void AsyncCopyFile( IAsyncResult ar )
   {
    int readedLength ;
   
    // Lock FileStream
    lock( stream )
    {
     readedLength = stream.EndRead( ar );   // When stream endread, get readed length
    }

    // Write to disk
    FileStream fsWriter = new FileStream( "C:\\copyedFile.bin" , FileMode.Append , FileAccess.Write );
    fsWriter.Write( buffer , 0 , buffer.Length );
    fsWriter.Close();

    // Current stream position
    position += readedLength;
     
    // Response UI
    MethodInvoker m = new MethodInvoker( SynchProgressBar );
    m.BeginInvoke( null , null );

    if ( position >= totalSize ) // Read over.
    {
     stream.Close();        //Close FileStream
     return ;
    }

    // Continue to read and write
    lock ( stream )
    {
     int leftSize = totalSize - position;

     if ( leftSize < BUFFER_SIZE )
      buffer = new byte[ leftSize ];

     stream.BeginRead( buffer , 0 , buffer.Length , new AsyncCallback( AsyncCopyFile ) , null );     
      
    }
   }

   private void SynchProgressBar()
   {
    this.progressBar1.Maximum = totalSize;
    this.progressBar1.Value   = position ;
   }

}
}
从其他地方搬过来的,希望有点帮助。
2010-01-23 13:52
快速回复:如何计算复制文件的进度并显示
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012572 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved