C# 利用系统API 复制大文件(显示进度条)

 
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.Runtime.InteropServices;
using System.IO;

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

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

        private void btn_select2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog f = new
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用C#编写的复制文件进度条示例代码: ```csharp using System; using System.IO; using System.Windows.Forms; public class FileCopyProgressBar : Form { private ProgressBar progressBar; private Button startButton; public FileCopyProgressBar() { Text = "File Copy Progress Bar"; progressBar = new ProgressBar(); progressBar.Dock = DockStyle.Top; Controls.Add(progressBar); startButton = new Button(); startButton.Text = "Copy File"; startButton.Dock = DockStyle.Bottom; startButton.Click += new EventHandler(OnStartButtonClicked); Controls.Add(startButton); } private void OnStartButtonClicked(object sender, EventArgs e) { string sourceFilePath = "path_to_source_file"; string destinationFilePath = "path_to_destination_file"; // Open the source file for reading using (FileStream sourceStream = new FileStream(sourceFilePath, FileMode.Open)) { // Create the destination file using (FileStream destinationStream = new FileStream(destinationFilePath, FileMode.Create)) { // Set the buffer size to 10KB byte[] buffer = new byte[10240]; int bytesRead; long totalBytesRead = 0; long fileSize = sourceStream.Length; // Copy the file and update the progress bar while ((bytesRead = sourceStream.Read(buffer, 0, buffer.Length)) > 0) { destinationStream.Write(buffer, 0, bytesRead); totalBytesRead += bytesRead; int percentComplete = (int)((double)totalBytesRead / fileSize * 100); progressBar.Value = percentComplete; Application.DoEvents(); } } } MessageBox.Show("File copied successfully!"); } public static void Main() { Application.Run(new FileCopyProgressBar()); } } ``` 在上面的示例代码中,我们创建了一个名为`FileCopyProgressBar`的窗体,其中包含一个`ProgressBar`和一个`Button`。当用户单击`Button`时,我们将复制文件到目标文件,并在进程中更新进度条。请注意,我们使用`Application.DoEvents()`来允许UI更新。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值