简体   繁体   中英

Copying File with Progress Bar in C# WPF

I am using the below code for Copying the files from one location to another

private void CopyFilesRecursivelyy(string serverDirectorty, string localDirectory)
   {
        foreach (string dirPath in Directory.GetDirectories(serverDirectorty, "*", SearchOption.AllDirectories))
        {
            Directory.CreateDirectory(dirPath.Replace(serverDirectorty, localDirectory));
        }
        string[] files = Directory.GetFiles(serverDirectorty, "*.*", SearchOption.AllDirectories);

        for (int i = 0; i < files.Length; i++)
        {
            string newPath = files[i];
            File.Copy(newPath, newPath.Replace(serverDirectorty, localDirectory), true);
          
        }
     
    }

Below is my XML code:

<ProgressBar Name ="pb" Grid.Column="2" HorizontalAlignment="Center" Height="16" Margin="0,0,0,0" VerticalAlignment="Center" Maximum="{Binding progressMax}" Value="{Binding intProgress}" Width="195" Visibility="{Binding visibilityPrgb}"/>

This is the code for Binding the Properties of Progress bar:

public int intProgress
    {
        get => _intProgress;
        set
        {
            _intProgress = value;
            OnPropertyChanged();
        }
    }
    private int _intProgress;
    public int progressMax
    {
        get => _progressMax;
        set
        {
            _progressMax = value;
            OnPropertyChanged();
        }
    }
    private int _progressMax;
    public bool visibilityPrgb
    {
        get => _visibilityPrgb;
        set
        {
            _visibilityPrgb = value;
            OnPropertyChanged();
        }
    }
    private bool _visibilityPrgb;

Can anyone tell me how can I integrate this progress bar with my above code. I want progress bar to run from 1-100 and after the completion of files copying I want to hidden the progress bar.

Set progressMax to the number of files to be copied and then increase intProgress for each copied file, eg:

private void CopyFilesRecursivelyy(string serverDirectorty, string localDirectory)
{
    foreach (string dirPath in Directory.GetDirectories(serverDirectorty, "*", SearchOption.AllDirectories))
    {
        Directory.CreateDirectory(dirPath.Replace(serverDirectorty, localDirectory));
    }
    string[] files = Directory.GetFiles(serverDirectorty, "*.*", SearchOption.AllDirectories);

    progressMax = files.Length;
    intProgress = 0;
    visibilityPrgb = true;

    for (int i = 0; i < files.Length; i++)
    {
        string newPath = files[i];
        File.Copy(newPath, newPath.Replace(serverDirectorty, localDirectory), true);
        intProgress++;
    }

    visibilityPrgb = false;

}

Also make sure that you are calling CopyFilesRecursivelyy on a background thread and that you use a converter to convert the value of the visibilityPrgb property to a Visibility in your view:

<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>
...
Visibility="{Binding visibilityPrgb,  Converter={StaticResource BooleanToVisibilityConverter}}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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