gpt4 book ai didi

c# - 如何让我的进度条显示正在复制的目录的进度

转载 作者:行者123 更新时间:2023-11-30 15:51:58 24 4
gpt4 key购买 nike

<分区>

我有一段代码将文件夹复制到另一个目录。我想实现一个进度条,以便用户可以看到正在复制的文件的进度。我已经尝试了一些方法,但就是无法让它发挥作用。

这是我认为可以执行我上面所说的代码,但是进度条没有取得任何的进展,为什么实际上正在复制文件夹。

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.Tasks;
using System.Windows.Forms;

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

private void Form1_Load(object sender, EventArgs e)
{

}

string Dirr;
string Dirr2;
int no = 0;
int Count = 0;
int Count2 = 0;
private void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
if (no == 0)
{
no = 1;
Dirr = sourceDirName;
Dirr2 = destDirName;
Count = Directory.GetFiles(Dirr, "*.*", SearchOption.AllDirectories).Length;
Count = 100 / Count;
}

// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);

if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}

DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}

// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
Count2 = Count2 + 1;
progressBar1.Value = Count2 * Count;
}

// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}

private void button1_Click(object sender, EventArgs e)
{
DirectoryCopy(@"C:\Users\Stefan\Downloads\Portable Python 2.7.15 Basic (x64)\Portable Python 2.7.15 x64", @"C:\Users\Stefan\Documents\Backuppers_Backups\Portable Python 2.7.15 x64", true);
MessageBox.Show("Done coppieng", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}

我希望进度条会缓慢上升,但事实并非如此:它保持在 0

但是它确实复制了文件

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com