Back to Devexpress

How to: Display File Operation's Progress via ProgressBarControl

windowsforms-9514-controls-and-libraries-editors-and-simple-controls-examples-how-to-display-file-operations-progress-via-progressbarcontrol.md

latest2.6 KB
Original Source

How to: Display File Operation's Progress via ProgressBarControl

  • Oct 25, 2019
  • 2 minutes to read

In the code fragment below, a DeleteFiles method removes all files contained within the directory specified by the source parameter. The ProgressBarControl is used to display the progress of file delete operations. The RepositoryItemProgressBar.Minimum and RepositoryItemProgressBar.Maximum properties are used to specify a range for the progress bar that is equivalent to the number of files to be removed. The code also uses the RepositoryItemProgressBar.Step property with the ProgressBarControl.PerformStep method, to increment the position of the progress bar as soon as a file is removed.

csharp
using System.IO;
using DevExpress.XtraEditors.Controls;
// ...
private void DeleteFiles(string source){
   if (Directory.Exists(source)){
      string[] fileEntries = Directory.GetFiles(source);
      // Initializing progress bar properties
      progressBarControl1.Properties.Step = 1;
      progressBarControl1.Properties.PercentView = true;
      progressBarControl1.Properties.Maximum = fileEntries.Length;
      progressBarControl1.Properties.Minimum = 0;
      // Removing the list of files found in the specified directory
      foreach(string fileName in fileEntries){
         File.Delete(fileName);
         progressBarControl1.PerformStep();
         progressBarControl1.Update();
      }
   }
}
// ...
DeleteFiles("d:\\Temp");
vb
Imports System.IO
Imports DevExpress.XtraEditors.Controls
' ...
Private Sub DeleteFiles(ByVal source As String)
   If Directory.Exists(source) Then
      Dim fileEntries As String() = Directory.GetFiles(source)
      ' Initializing progress bar properties
      progressBarControl1.Properties.Step = 1
      progressBarControl1.Properties.PercentView = True
      progressBarControl1.Properties.Maximum = fileEntries.Length
      progressBarControl1.Properties.Minimum = 0
      ' Removing the list of files found in the specified directory
      For Each fileName As String In fileEntries
         File.Delete(fileName)
         progressBarControl1.PerformStep()
         progressBarControl1.Update()
      Next fileName
   End If
End Sub
' ...
DeleteFiles("d:\Temp")