在vs中用文件流复制文件.docx

上传人:太** 文档编号:96751869 上传时间:2024-03-18 格式:DOCX 页数:9 大小:13.75KB
返回 下载 相关 举报
在vs中用文件流复制文件.docx_第1页
第1页 / 共9页
在vs中用文件流复制文件.docx_第2页
第2页 / 共9页
点击查看更多>>
资源描述

《在vs中用文件流复制文件.docx》由会员分享,可在线阅读,更多相关《在vs中用文件流复制文件.docx(9页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、using System;using System. Collections. Generic;using System. ComponentModel;using System. Data;using System. Drawing;using System. Linq;using System. Text;using System. Windows. Forms;using System. 10;using System. Threading;线程的命名空间namespace CopyFileByThread(public partial class Forml : Form(public

2、 Forml ()InitializeComponent();private System. Threading. Thread thdAddFile; /创建一个线 程. . . . private string str =;FileStream FormerOpen;实例化 FileStream 类FileStream ToFileOpen;实例化 FileStream 类private void buttonl_Click(object sender, EventArgs e)if (openFileDialogl. ShowDialog()=DialogResult. OK) /打开文

3、件对话框textBoxl. Text = openFileDialogl. FileName;/获取 源文件的路径private void button2_Click (object sender, EventArgs e)(if (folderBrowserDialogl. ShowDialog()二二DialogResult. OK)打开文件夹对话框textBox2.Text =folderBrowserDialogl. SelectedPath; 获取目的文件的路径private void button3 Click (object sender, EventArgs e)if (tex

4、tBoxl. Text. Length = 0 | textBox2. Text. LengthMessageBox. Show(请选择原文件路径或者目的文件路径。);return ;str = textBoxl. Text;记录源文件的路径str 二 + str. Substring(str. LastlndexOf C ) + 1, str. Length - str. LastlndexOf (,) - 1) ;/获取源文件的名称thdAddFile = new Thread (newThreadStart (SetAddFile);创建一个线程public delegate void

5、AddFile();定义委托/在线程上执行委托/public void SetAddFile()this. Invoke (new AddFi 1 e (RunAddFi 1 e); 在线程上执行 指定的委托/对文件进行复制,并在复制完成后关闭线程/public void RunAddFile()(CopyFile(textBoxl. Text, textBox2. Text + str, 1024, progressBarl); 复制文件/文件的复制/源文件路径/目的文件路径/传输大小/ ProgressBar 控件public void CopyFile(string FormerFile

6、, string toFile, int SectSize, ProgressBar progressBarl)(progressBarl. Value = 0;设置进度栏的当前位置为0progressBarl. Minimum = 0;/设置进度栏的最小值为0FileStream fileToCreate = new FileStream(toFile,FileMode. Create);创建目的文件,如果已存在将被覆盖fileToCreate. Close。;关闭所有资源fileToCreate. Dispose ();释放所有资源FormerOpen = new FileStream(F

7、ormerFi1e, FileMode. Open, FileAccess. Read);以只读方式打开源文件ToFileOpen = new FileStream(toFile, FileMode. Append, FileAccess. Write);以写方式打开目的文件int max 二Convert. Tolnt32(Math. Ceiling(double)FormerOpen. Length / (double) SectSize); 根据一次传输的大小,计算传输的个数progressBarl. Maximum = max;设置进度栏的最大值int FileSize;要拷贝的文件的

8、大小if (SectSize FormerOpen. Length)如果分段拷贝,即 每次拷贝内容小于文件总长度byte buffer = new byteSectSize;根据传输的 大小,定义一个字节数组int copied=0;记录传输的大小int tem_n=1 ;设置进度栏中进度块的增加个数while (copied = (int)FormerOpen. Length - SectSize)”拷贝主体部份FileSize = FormerOpen. Read (buffer, 0, SectSize);从0开始读,每次最大读SectSizeFormerOpen. Flush ();清

9、空缓存ToFileOpen. Write (buffer, 0, SectSize); 向 I的文件写入字节ToFileOpen. Flush();清空缓存ToFileOpen.Position = FormerOpen. Position;/ 使源文件和目的文件流的位置相同copied += FileSize; 记录已拷贝的大小progressBarl. Value = progressBarl. Value + tem_n; 增加进度栏的进度块int left = (int)FormerOpen. Length - copied;获 取剩余大小FileSize = FormerOpen.

10、Read(buffer, 0, left);/ 读取剩余的字节FormerOpen. Flush () ;/清空缓存ToFileOpen. Write (buffer, 0, left);写入剩余的部 分ToFileOpen. Flush();清空缓存else如果整体拷贝,即每次拷贝内容大于文件总长度byte buffer = new byteFormerOpen. Length;/ 获取文件的大小FormerOpen. Read(buffer, 0,(int) FormerOpen. Length); 读取源文件的字节FormerOpen. Flush () ;/清空缓存ToFileOpen. Write(buffer, 0,(int) FormerOpen. Length); 写放字节ToFileOpen. Flush();清空缓存)FormerOpen. Close (); 释放所有资源ToFileOpen. Close。;释放所有资源if (MessageBox. Show(复制完成)=DialogResult. OK) /显示复制完成提示对话框(progressBarl. Value = 0;设置进度栏的当有位置为 0textBoxl. Clear ();清空文本textBox2. Clear ();str =

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 工作报告

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com