Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am willing to copy/move files from one folder to another by selecting the Path of the source folder and destination folder in my view.

I have done the code and I can select the paths but I cannot get them moving files or copying. It only moves one file using static method "MoveTo"

I am using WPF MVVM Prism and I have struggled for 3 full days to get this working. I have thousands files I receive a day so I am working on doing the simplest way for users to work on.

The code I posted below is my ViewModel then I am binding these buttons and Textboxs in my View

What I have tried:

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);
            }
        }
    }
}
Posted
Updated 28-Feb-20 6:27am
Comments
Richard MacCutchan 28-Feb-20 9:00am    
You need to use your debugger to chack how many files are being returned by DirectoryInfo(source).GetFiles(). If it is more than one then check what actually happens in the loop.
Member 13924789 28-Feb-20 13:09pm    
Will give this a try
George Swan 3-Mar-20 4:51am    
Try using Directory.EnumerateFiles instead of GetFiles. Also I think you need to be on the UI thread to popup a message box so it's not wise to do it in on a worker thread. My preference is to use the Tasked-Based Asynchronous pattern rather than BackgroundWorker, there is a good example here https://docs.microsoft.com/en-us/dotnet/standard/io/asynchronous-file-i-o

1 solution

"Do_Work" typically involves a "loop" that runs until some condition is met.

You don't have a "loop"; you're simply handling settings for a single file. You need a "queue" of files for the background worker (or something similar).

The worker is started anytime it is idle and file names are added to the queue.
 
Share this answer
 
Comments
Richard MacCutchan 28-Feb-20 12:48pm    
What about the foreach statement?
Member 13924789 28-Feb-20 13:08pm    
I am thinking that will help me because I once moved a folder name. It should be something a sort of :-

string [] files = Directory.GetFiles(source)
foreach(var file in files)
{
}

Thanks I will give it a try over there and let you know

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900