简体   繁体   English

使用进度条将文件从一个目录复制到另一个目录 C# WPF

[英]Copying Files From One Directory to Another With Progress bar C# WPF

I am using the Following Code for Copying the Files from one directory to Another Directory我正在使用以下代码将文件从一个目录复制到另一个目录

private void CopyFilesRecursively(string serverDirectorty, string localDirectory)
    {
        serverDirectorty = settings["baseDocPathSource"] as string;
        localDirectory = settings["baseDocPath"] as string;

        //Now Create all of the directories
        foreach (string dirPath in Directory.GetDirectories(serverDirectorty, "*", SearchOption.AllDirectories))
        {
            Directory.CreateDirectory(dirPath.Replace(serverDirectorty, localDirectory));
        }

        //Copy all the files & Replaces any files with the same name
        foreach (string newPath in Directory.GetFiles(serverDirectorty, "*.*", SearchOption.AllDirectories))
        {
            File.Copy(newPath, newPath.Replace(serverDirectorty, localDirectory), true);
        }
    }

I want to Integrate the Progress bar to my code ie when I click the copy button I want to show the progress bar moving from 0 to 100 and when copying of the files is done I want to hide the progress bar.我想将进度条集成到我的代码中,即,当我单击复制按钮时,我想显示进度条从 0 移动到 100,并且当文件复制完成时,我想隐藏进度条。

Below is my XML File下面是我的 XML 文件

    <Grid>
    <ProgressBar Visibility="Hidden" Name="pbCopy" HorizontalAlignment="Left" Height="65" Margin="127,151,0,0" VerticalAlignment="Top" Width="485"/>
    <Button Content="Copy Files" HorizontalAlignment="Left" Margin="283,253,0,0" VerticalAlignment="Top" Width="164" Height="66"/>

</Grid>

I want to Hide the Progress Bar from my Form Initially and want to set the Visibility "Visible" after clicking on the Button.我想最初从我的表单中隐藏进度条,并希望在单击按钮后设置可见性“可见”

Try to use the Progress class.尝试使用进度class。 Pass it to your method.将其传递给您的方法。

private void CopyFilesRecursively(string serverDirectorty, string localDirectory, IProgress<double> progress)
{
    serverDirectorty = settings["baseDocPathSource"] as string;
    localDirectory = settings["baseDocPath"] as string;

    //Now Create all of the directories
    foreach (string dirPath in Directory.GetDirectories(serverDirectorty, "*", SearchOption.AllDirectories))
    {
       Directory.CreateDirectory(dirPath.Replace(serverDirectorty, localDirectory));
    }

    //Copy all the files & Replaces any files with the same name
    var files = Directory.GetFiles(serverDirectorty, "*.*", SearchOption.AllDirectories);

    double processed = 0;
    //Reset progress value to 0.
    progress?.Report(0);
            
    foreach (string newPath in files)
    {
        File.Copy(newPath, newPath.Replace(serverDirectorty, localDirectory), true);
        //Report in percentage 1-100%. If you have a ton of files, you should reduce the number of calling progress?.Report() e.g. using dividing by modulo.
        progress?.Report((++processed / files.Length) * 100);
    }
            
    //If files weren't found.
    progress?.Report(100);
}

Give the ProgressBar a name in your XAML markup:在 XAML 标记中为ProgressBar命名:

<ProgressBar x:Name="pb" ... />

...and set its properties in your method. ...并在您的方法中设置其属性。 Something like this:像这样的东西:

private void CopyFilesRecursively(string serverDirectorty, string localDirectory)
{
    serverDirectorty = settings["baseDocPathSource"] as string;
    localDirectory = settings["baseDocPath"] as string;

    //Now Create all of the directories
    foreach (string dirPath in Directory.GetDirectories(serverDirectorty, "*", SearchOption.AllDirectories))
    {
        Directory.CreateDirectory(dirPath.Replace(serverDirectorty, localDirectory));
    }

    //Copy all the files & Replaces any files with the same name
    string[] files = Directory.GetFiles(serverDirectorty, "*.*", SearchOption.AllDirectories);
    pb.Dispatcher.Invoke(() =>
    {
        pb.Visibility = Visibility.Visible;
        pb.Maximum = files.Length;
        pb.Value = 0;
    });
    for (int i = 0; i < files.Length; i++)
    {
        string newPath = files[i];
        File.Copy(newPath, newPath.Replace(serverDirectorty, localDirectory), true);
        pb.Dispatcher.Invoke(() => pb.Value += i + 1);
    }
    pb.Dispatcher.Invoke(() => pb.Visibility = Visibility.Collapsed);
}

Also make sure that you are running your copy method on a background thread:还要确保您在后台线程上运行您的复制方法:

private void Button_Click(object sender, RoutedEventArgs e)
{
    Task.Run(() => CopyFilesRecursively("...", "..."));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM