'show progress during renaming and moving image files using WPF c#
I am currently having problems showing a progress bar while waiting for image files to be renamed. I've tried several progress bar tutorials and answers but am having trouble plugging them into my project.
Here's the code I have so far:(I am very much a novice) I removed the progress bar for now since i can't get it to work.
using System;
using System.IO;
using System.Windows;
namespace Change_Image_file_name
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
string sourceFolder = @"C:\USB Archive Photos";
string destinationFolder = @"C:\New USB Archive\";
private void Button_Click(object sender, RoutedEventArgs e)
{
if (Directory.Exists(sourceFolder))
{
DirectoryInfo d = new DirectoryInfo(sourceFolder);
int i = 1;
if (Directory.Exists(destinationFolder))
{
Array.ForEach(Directory.GetFiles(destinationFolder), File.Delete);
foreach (var file in d.GetFiles())
{
string imageName = i.ToString();
Directory.Move(file.FullName, destinationFolder + imageName + ".jpg");
i++;
}
MessageBox.Show("The images have been renamed and moved to the New USB Archive folder");
}
else
{
MessageBox.Show("Please add a New USB Archive folder on the root of C:");
}
}
else
{
MessageBox.Show("Please add a USB Archive Photos folder on the root of C:");
}
}
}
}
I added a progress bar and it seems to work, but not sure if this is really the correct way to implement it.
namespace progress_test
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void DoWorkButton_Click(object sender, RoutedEventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.WorkerReportsProgress = true;
worker.DoWork += worker_DoWork;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerAsync();
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
TestProgressBar.Value = e.ProgressPercentage;
ProgressTextBlock.Text = (string)e.UserState;
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
var worker = sender as BackgroundWorker;
worker.ReportProgress(0, String.Format("Processing Iteration 1."));
for (int i = 0; i < 10; i++)
{
Thread.Sleep(1000);
worker.ReportProgress((i + 1) * 10, String.Format("Processing Iteration {0}.", i + 2));
}
worker.ReportProgress(100, "Done Processing.");
string sourceFolder = @"C:\USB Archive Photos";
string destinationFolder = @"C:\New USB Archive\";
//private void Button_Click(object sender, RoutedEventArgs e)
{
if (System.IO.Directory.Exists(sourceFolder))
{
DirectoryInfo d = new DirectoryInfo(sourceFolder);
int i = 1;
if (Directory.Exists(destinationFolder))
{
Array.ForEach(Directory.GetFiles(destinationFolder), File.Delete);
foreach (var file in d.GetFiles())
{
string imageName = i.ToString();
Directory.Move(file.FullName, destinationFolder + imageName + ".jpg");
i++;
}
MessageBox.Show("The images have been renamed and moved to the New USB Archive folder");
}
else
{
MessageBox.Show("Please add a New USB Archive folder on the root of C:");
}
}
else
{
MessageBox.Show("Please add a USB Archive Photos folder on the root of C:");
}
}
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//MessageBox.Show("All Done!");
TestProgressBar.Value = 0;
ProgressTextBlock.Text = "";
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
