C# 在复制文件时使用进度条
C# working with progress bar while copying a file
有没有人有在复制文件时使用进度条的示例,或者可以指导我到提出这个问题的地方?
private void Transferfiles(DirectoryInfo source, DirectoryInfo target)
{
int e = 0
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
foreach (FileInfo eachhfile in source.GetFiles())
{
eachhfile.CopyTo(Path.Combine(target.ToString(), eachhfile .Name));
BytesToKilobytes += ((eachhfile .Length / 1024) / 1024);
e = BytesToKilobytes ;
backgroundWorker1.ReportProgress(e);
}
foreach (DirectoryInfo SubDirectory in source.GetDirectories())
{
DirectoryInfo newTargetDirectory =
target.CreateSubdirectory(diSourceSubDir.Name);
Transferfiles(SubDirectory, newTargetDirectory );
}
}
以上是我目前使用的代码。它有效但并没有真正给我我想要的东西。我正在寻找一种方法让进度条随着文件复制而更新,这样进度条就会一直移动直到文件复制完成。
(可能我误会你了,因为你想在单个大文件上继续移动?如果是这样,我没有快速的解决方案给你,甚至 Windows 也无法管理它文件浏览器)
MSDN 是你的朋友:
https://msdn.microsoft.com/de-de/library/system.windows.forms.progressbar(v=vs.110).aspx
private void CopyWithProgress(string[] filenames)
{
// Display the ProgressBar control.
pBar1.Visible = true;
// Set Minimum to 1 to represent the first file being copied.
pBar1.Minimum = 1;
// Set Maximum to the total number of files to copy.
pBar1.Maximum = filenames.Length;
// Set the initial value of the ProgressBar.
pBar1.Value = 1;
// Set the Step property to a value of 1 to represent each file being copied.
pBar1.Step = 1;
// Loop through all files to copy.
for (int x = 1; x <= filenames.Length; x++)
{
// Copy the file and increment the ProgressBar if successful.
if(CopyFile(filenames[x-1]) == true)
{
// Perform the increment on the ProgressBar.
pBar1.PerformStep();
}
}
}
您需要自己实现复制代码(您不能使用 FileCopy
或类似的命令)。有关如何执行此操作的示例,请参阅 this article。
您可以检查要移动到的文件夹的大小,并将其用作进度条的当前值。
有没有人有在复制文件时使用进度条的示例,或者可以指导我到提出这个问题的地方?
private void Transferfiles(DirectoryInfo source, DirectoryInfo target)
{
int e = 0
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
foreach (FileInfo eachhfile in source.GetFiles())
{
eachhfile.CopyTo(Path.Combine(target.ToString(), eachhfile .Name));
BytesToKilobytes += ((eachhfile .Length / 1024) / 1024);
e = BytesToKilobytes ;
backgroundWorker1.ReportProgress(e);
}
foreach (DirectoryInfo SubDirectory in source.GetDirectories())
{
DirectoryInfo newTargetDirectory =
target.CreateSubdirectory(diSourceSubDir.Name);
Transferfiles(SubDirectory, newTargetDirectory );
}
}
以上是我目前使用的代码。它有效但并没有真正给我我想要的东西。我正在寻找一种方法让进度条随着文件复制而更新,这样进度条就会一直移动直到文件复制完成。
(可能我误会你了,因为你想在单个大文件上继续移动?如果是这样,我没有快速的解决方案给你,甚至 Windows 也无法管理它文件浏览器)
MSDN 是你的朋友:
https://msdn.microsoft.com/de-de/library/system.windows.forms.progressbar(v=vs.110).aspx
private void CopyWithProgress(string[] filenames)
{
// Display the ProgressBar control.
pBar1.Visible = true;
// Set Minimum to 1 to represent the first file being copied.
pBar1.Minimum = 1;
// Set Maximum to the total number of files to copy.
pBar1.Maximum = filenames.Length;
// Set the initial value of the ProgressBar.
pBar1.Value = 1;
// Set the Step property to a value of 1 to represent each file being copied.
pBar1.Step = 1;
// Loop through all files to copy.
for (int x = 1; x <= filenames.Length; x++)
{
// Copy the file and increment the ProgressBar if successful.
if(CopyFile(filenames[x-1]) == true)
{
// Perform the increment on the ProgressBar.
pBar1.PerformStep();
}
}
}
您需要自己实现复制代码(您不能使用 FileCopy
或类似的命令)。有关如何执行此操作的示例,请参阅 this article。
您可以检查要移动到的文件夹的大小,并将其用作进度条的当前值。