• 大小: 0.04M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2024-04-29
  • 语言: C#
  • 标签: 大文件  复制  文件  

资源简介

大文件复制 多线程 c#

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CopyFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender EventArgs e)
        {
            InitialProgressBar();
        }
        //初始化进度条
        private void InitialProgressBar()
        {
            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;
            progressBar1.Value = 0;
        }

        private void copy_Click(object sender EventArgs e)
        {
            //创建子线程来拷贝大文件
            Thread t = new Thread(new ThreadStart(() =>
              {
                  string source = sourcebox.Text.Trim();
                  string target = targetbox.Text.Trim();
                  BigFileCopy(source target);
              }));
            t.Start();

        }

        private void BigFileCopy(string sourcestring target)
        {
            using(FileStream fsReader=new FileStream(sourceFileMode.OpenFileAccess.Read))
            {
                using(FileStream fsWriter=new FileStream(targetFileMode.CreateFileAccess.Write))
                {
                    byte[] array = new byte[1024 * 1024];
                    //读取字节块数据写入缓冲区
                    int count = fsReader.Read(array 0 array.Length);
                    while(count>0)
                    {
                        //将字节块写入文件流
                        fsWriter.Write(array 0 count);
                        count = fsReader.Read(array 0 array.Length);
                        int Value = (int)(fsWriter.Position * 1.0 / fsReader.Length * 100);
                        //调用invoke方法避免线程冲突
                        progressBar1.Invoke(new Action((u) => {
                            progressBar1.Value = u;
                        })Value);
                    }
                }
                MessageBox.Show(“OK“);
            }
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2020-05-29 16:51  CopyFile\.vs\
     目录           0  2020-05-29 16:51  CopyFile\.vs\CopyFile\
     目录           0  2020-05-29 16:51  CopyFile\.vs\CopyFile\v16\
     文件       43520  2020-06-07 10:00  CopyFile\.vs\CopyFile\v16\.suo
     文件         189  2020-05-29 16:51  CopyFile\App.config
     目录           0  2020-05-29 16:51  CopyFile\bin\
     目录           0  2020-05-29 17:58  CopyFile\bin\Debug\
     文件       10240  2020-06-07 09:41  CopyFile\bin\Debug\CopyFile.exe
     文件         189  2020-05-29 16:51  CopyFile\bin\Debug\CopyFile.exe.config
     文件       32256  2020-06-07 09:41  CopyFile\bin\Debug\CopyFile.pdb
     文件        3604  2020-05-29 17:01  CopyFile\CopyFile.csproj
     文件        1121  2020-05-29 16:51  CopyFile\CopyFile.sln
     文件        2344  2020-06-07 09:41  CopyFile\Form1.cs
     文件        5147  2020-05-29 17:57  CopyFile\Form1.Designer.cs
     文件        5817  2020-05-29 17:57  CopyFile\Form1.resx
     目录           0  2020-05-29 16:51  CopyFile\obj\
     目录           0  2020-06-07 09:41  CopyFile\obj\Debug\
     文件          42  2020-05-29 17:58  CopyFile\obj\Debug\CopyFile.csproj.CoreCompileInputs.cache
     文件         585  2020-05-29 17:58  CopyFile\obj\Debug\CopyFile.csproj.FileListAbsolute.txt
     文件        1012  2020-05-29 17:58  CopyFile\obj\Debug\CopyFile.csproj.GenerateResource.cache
     文件        2379  2020-06-07 09:41  CopyFile\obj\Debug\CopyFile.csprojAssemblyReference.cache
     文件       10240  2020-06-07 09:41  CopyFile\obj\Debug\CopyFile.exe
     文件         180  2020-05-29 17:58  CopyFile\obj\Debug\CopyFile.Form1.resources
     文件       32256  2020-06-07 09:41  CopyFile\obj\Debug\CopyFile.pdb
     文件         180  2020-05-29 17:58  CopyFile\obj\Debug\CopyFile.Properties.Resources.resources
     文件        1435  2020-05-29 17:01  CopyFile\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        7084  2020-05-29 16:51  CopyFile\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     目录           0  2020-05-29 16:51  CopyFile\obj\Debug\TempPE\
     文件         520  2020-05-29 16:51  CopyFile\Program.cs
     目录           0  2020-05-29 16:51  CopyFile\Properties\
     文件        1308  2020-05-29 16:51  CopyFile\Properties\AssemblyInfo.cs
............此处省略4个文件信息

评论

共有 条评论