[懵懂边缘]使用C#在进度条中显示复制文件的进度

C#读取文件的实时进度条 C#读取文件的实时进度条对线程及其线程间通讯,线程访问UI控件的问题没有任何经验,通过编写读取文件的实时进度条来增加一些此方面的基础知识。 写过一个用Redis存取数据的小项目,Redis的持久化机制导致在机器crash掉的时候可能会丢失数据,项目也有把检测到的数据写入txt的需求,由于数据txt写入是实时的(每获取一条记录,就写入一条),所以机器crash掉的时候,数据的恢复可以通过读取 阅读详情
 

Code List:
-------------------------------------------------------------------------

/*****************************************************************
** 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 System.IO ;
using System.Xml ;
using System.Collections ;
using System.Reflection ;
using System.Text ;
using System.Data ;
using System.ComponentModel;
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 System.ComponentModel.Container 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 ;
}

}
}



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=728405

C# WinForm多图片上传带进度条demo C# WinForm多图片上传带进度条 阅读详情

相关推荐

c#实现文件读取并显示读取文件进度条

C#写的文件读取功能,根据文件已读取数据同步显示已读取文件进度条

使用C#进度条显示复制文件进度

Code List:-------------------------------------------------------------------------/******************************************************************* File Name:       frmMain.cs** Copyright (c) 1999

OneDotRed的专栏 4457

c# 上传文件进度条

文件下载进度条,自己记得在nuget中下载Jquery和Jquery UI ,文件上传进度条.net c#版本demo

C#进度条显示上传文件代码

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using

1776

C#:复制文件显示进度条

1.窗口界面 主要是文本框textBox、按钮button、进度条prograssBar三大组件所组成的。 2.完整代码 using System; using System.IO; using System.Windows.Forms; using System.Threading;//线程序的命名空间 using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; namespace 复制文件显示

qq_37435116的博客 1967

[C#]实现文件复制[更新]实时显示进度条

复制更新文件时,如果想要实时的获得文件复制进度,并显示在窗体的进度条上有许多方法,在寻找了一段资料后,我整理了一些别人的代码,获得到本篇的代码,代码实现了根据配置文件,复制文件列表的效果.思路其实很简单,就是异步的读取和写入流,在异步回调的方法中,响应窗体的进度条控件,所有注释都写在代码中了. 相关代码:http://download.csdn.net/source/880686程

轩辕寒星的专栏 1万+

WebClient 上传进度条控件

1、界面:   2、代码: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Net; name...

weixin_34409357的博客 255

C#实现文件上传进度条

1、前台代码FileProcessBar.aspx: <%@ page Language="C#" AutoEventWireup="true" CodeFile="FileProcessBar.aspx.cs" EnableEventValidation="false" Inherits="FileProcessBar" %> <!DOCTYpE html pUBL...

weixin_30416497的博客 1816

C#文件上传,实时更新进度条(非多线程实现)

实现的是 主文件夹下(包含子文件夹)多个.txt文件的读取进度显示。 按下read后开始读取文件夹下所有文本文件。 按下browse 弹出文件对话框 进度条的初始化 代码下载: ...

fxm396_的博客 2020

C#复制文件显示进度条

摘要:C#源码,文件操作,复制文件,进度条  C#进度条复制文件功能演示,对文件进行复制,并在复制完成后关闭线程。如果分段拷贝,即每次拷贝内容小于文件总长度,根据传输的大小,定义一个字节数组,记录传输的大小,设置进度栏中进度块的增加个数,若复制完成,显示"复制完成"提示对话框。

简单C#winform多线程委托调用进度条

简单winform多线程调用进度条的例子 BeginInvoke

c#上传大文件控件(包含进度条

上传大文件控件(包含进度条) zh-CN" UICulture="zh-CN" Language="C#" -->   上传进度条控件 Web.config 配置

自由程序员(网络偏执狂)- epzk 4923

c#文件上传 前端ajax显示进度条

记录下 今天的研究笔记。 前端页面 @{ ViewBag.Title = "Home Page"; } <style type="text/css"> .main { padding: 20px 28px; margin: 0 auto; } .progressNum { ...

chencong2005100的博客 961

C# 程序检测更新 下载 + 进度条

/** * DownloadFile1(url, savePath, progressBar1, progress,speed); * URL 文件下载地址 * savePath 下载的文件存放路径 * prog 进度条 工具箱去找 progressBar1,显示进度条 * progress label 设置的name值是progress,显示进度百分比列 ..

weixin_42674576的博客 1213

winFrom+Ftp的多文件上传及其上传进度

 因为项目要求,制作的一个多文件上传,并显示进度条一段代码(vs2005环境)。(只为粗略的实现,代码并不规范)当多个文件上传的时候,需要依次队列形式一个个上传,当上传某个文件的时候,锁定进程,上传完毕再开启锁。在主类中的上传按钮事件代码: //获取openFileDialog控件选择的文件名数组(openFileDialog可多个文件选择)        private void

1418

C# 进度条使用

C# 进度条使用,Winform自定义使用进度条

杰西笔记 1万+
上一篇: [mqt_2003的专栏]C#若干知识点的相关小程序
下一篇: 用Visual C#编写仿MSN Messager的滚动提示窗口
MaybeHelios
博客等级 码龄21年 65粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值