C#可视化Windows窗体应用程序(复制文本)

将一个txt文本的内容复制到另一个txt文本中。

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.IO;

namespace FileCopyPlan
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        public void CopyFile(string FormerFile, string toFile, int SectSize, ProgressBar progressBar1)
        {
            progressBar1.Value = 0;
            progressBar1.Minimum = 0;
            FileStream fileToCreate = new FileStream(toFile, FileMode.Create);
            fileToCreate.Close();
            fileToCreate.Dispose();
            FileStream FormerOpen = new FileStream(FormerFile, FileMode.Open, FileAccess.Read);
            FileStream ToFileOpen = new FileStream(toFile, FileMode.Append, FileAccess.Write);
            int max = Convert.ToInt32(Math.Ceiling((double)FormerOpen.Length / (double)SectSize));
            progressBar1.Maximum = max;
            int FileSize;
            if (SectSize < FormerOpen.Length)
            {
                byte[] buffer = new byte[SectSize];
                int copied = 0;
                int tem_n = 1;
                while (copied <= ((int)FormerOpen.Length - SectSize))
                {
                    FileSize = FormerOpen.Read(buffer, 0, SectSize);
                    FormerOpen.Flush();
                    ToFileOpen.Write(buffer, 0, SectSize);
                    ToFileOpen.Flush();
                    ToFileOpen.Position = FormerOpen.Position;
                    copied += FileSize;
                    progressBar1.Value = progressBar1.Value + tem_n;
                }
                int left = (int)FormerOpen.Length - copied;
                FileSize = FormerOpen.Read(buffer, 0, left);
                FormerOpen.Flush();
                ToFileOpen.Write(buffer, 0, left);
                ToFileOpen.Flush();
            }
            else
            {
                byte[] buffer = new byte[FormerOpen.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)
            {
                progressBar1.Value = 0;
                textBox1.Clear();
                textBox2.Clear();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = openFileDialog2.FileName;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CopyFile(textBox1.Text, textBox2.Text, 1, progressBar1);
        }
    }
}

运行结果:在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值