class MoveViewModel : BindableBase { public string Title { get; internal set; } readonly BackgroundWorker worker = new BackgroundWorker(); private string to; private string from; private int progressBar; public int Progress { get { return progressBar; } set { SetProperty(ref progressBar, value); } } public string From { get { return from; } set { SetProperty(ref from, value); } } public string To { get { return to; } set { SetProperty(ref to, value); } } public ICommand Click_From { get; private set; } public ICommand Click_To { get; private set; } public ICommand Click_Move { get; private set; } public MoveViewModel() { Click_From = new DelegateCommand(ClickFrom); Click_To = new DelegateCommand(ClickTo); Click_Move = new DelegateCommand(ClickMove); worker.WorkerSupportsCancellation = true; worker.WorkerReportsProgress = true; worker.DoWork += Worker_DoWork; worker.RunWorkerCompleted += Worker_RunWorkerCompleted; worker.ProgressChanged += Worker_ProgressChanged; } private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e) { Progress = e.ProgressPercentage; } private void ClickFrom() { try { FolderBrowserDialog dialog = new FolderBrowserDialog(); if(dialog.ShowDialog() == DialogResult.OK) { From = dialog.SelectedPath; } } catch (Exception e) { MessageBox.Show(e.Message); } } private void ClickTo() { try { FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == DialogResult.OK) { To = Path.Combine(fbd.SelectedPath, Path.GetFileName(From)); } } catch (Exception e) { MessageBox.Show(e.Message); } } void MoveFile(string source, string destination) { try { if(Directory.Exists(source)) { foreach(var file in new DirectoryInfo(source).GetFiles()) { file.MoveTo(destination); } } } catch (Exception e) { System.Windows.MessageBox.Show(e.Message); } } private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { } private void Worker_DoWork(object sender, DoWorkEventArgs e) { string source = From; string destination = To; MoveFile(source, destination); } private void ClickMove() { try { worker.RunWorkerAsync(); } catch(Exception e) { MessageBox.Show(e.Message); } } } }
DirectoryInfo(source).GetFiles()
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)