简体   繁体   中英

will only copy first item in the array c#

Hi I am trying to write a simple program to copy a folder from one soure to many in parallel. I am learning c# so have been trying to understand and change code examples, as i figured this the best way to learn somthing new.

The example below does not work as it only copies to the first destination in the destinationPaths

The stange thing is i have a simlar method to copy one file to many and this works everytime have i missing something?? i would be greatful if someone could tell me why this is not working i am guessing that there maybe certain things you can't do in parallel

any advice would be great

public  void CopyMultipleFolder(string sourceFilePath, params string[] destinationPaths)
    {
        if (string.IsNullOrEmpty(sourceFilePath)) MessageBox.Show("A source file must be specified.", "sourceFilePath");
        else
        {

            if (destinationPaths == null || destinationPaths.Length == 0) MessageBox.Show("At least one destination file must be specified.", "destinationPaths");
            else
            {
                try
                {



                    FileIOPermission writeAccess = new FileIOPermission(FileIOPermissionAccess.AllAccess, destinationPaths);
                    foreach (string i in destinationPaths)
                    {
                        writeAccess.AddPathList(FileIOPermissionAccess.Write, i);
                    }

                    writeAccess.Demand();






                    NetworkCredential user = new NetworkCredential();
                    user.UserName = Properties.Settings.Default.username;
                    user.Password = Properties.Settings.Default.password;

                    if (user.Password.Length == 0 || user.UserName.Length == 0)
                    {
                        MessageBox.Show("No Username or password have been entered click username on menu bar to update", "Update Credentials");
                    }
                    else
                    {

                        Parallel.ForEach(destinationPaths, new ParallelOptions(),
                                         destinationPath =>
                                         {


                                             if (sourceFilePath.EndsWith("*"))
                                             {
                                                 int l = sourceFilePath.Length - 4;

                                                 sourceFilePath = sourceFilePath.Remove(l);
                                             }
                                             else
                                             {

                                                 using (new NetworkConnection(destinationPath, user))
                                                 {
                                                     if (Directory.Exists(destinationPath + "\\" + foldername))
                                                     {
                                                         if (destinationPath.EndsWith("\\"))
                                                         {
                                                             DialogResult r = MessageBox.Show("Folder already Exists " + destinationPath + foldername + " Do You Want To overwrite All Files And Sub Folders", "Overwrite?", MessageBoxButtons.YesNo);


                                                             if (r == DialogResult.Yes)
                                                             {
                                                                 PleaseWait.Create();

                                                                 foreach (string dirPath in Directory.GetDirectories(sourceFilePath, "*", SearchOption.AllDirectories))
                                                                     Directory.CreateDirectory(dirPath.Replace(sourceFilePath, destinationPath + "\\" + foldername));


                                                                 foreach (string newPath in Directory.GetFiles(sourceFilePath, "*.*", SearchOption.AllDirectories))
                                                                     File.Copy(newPath, newPath.Replace(sourceFilePath, destinationPath+ "\\" + foldername), true);


                                                                 list = list + destinationPath + foldername + Environment.NewLine;
                                                             }
                                                             else
                                                             {
                                                             }

                                                         }
                                                         else
                                                         {


                                                             DialogResult r = MessageBox.Show("Folder already Exists " + destinationPath + "\\" + foldername + " Do you Want to overwrite All Files And SubFolders", "Overwrite?", MessageBoxButtons.YesNo);

                                                             if (r == DialogResult.Yes)
                                                             {
                                                                 PleaseWait.Create();

                                                                 foreach (string dirPath in Directory.GetDirectories(sourceFilePath, "*", SearchOption.AllDirectories))
                                                                     Directory.CreateDirectory(dirPath.Replace(sourceFilePath, destinationPath + "\\" + foldername));

                                                                 //Copy all the files
                                                                 foreach (string newPath in Directory.GetFiles(sourceFilePath, "*.*", SearchOption.AllDirectories))
                                                                     File.Copy(newPath, newPath.Replace(sourceFilePath, destinationPath + "\\" + foldername), true);


                                                                 list = list + destinationPath + "\\" + foldername + Environment.NewLine;
                                                             }
                                                             else
                                                             {
                                                             }
                                                         }
                                                     }
                                                     else
                                                     {
                                                         PleaseWait.Create();

                                                         foreach (string dirPath in Directory.GetDirectories(sourceFilePath, "*", SearchOption.AllDirectories))
                                                             Directory.CreateDirectory(dirPath.Replace(sourceFilePath, destinationPath + "\\" + foldername));

                                                         //Copy all the files
                                                         foreach (string newPath in Directory.GetFiles(sourceFilePath, "*.*", SearchOption.AllDirectories))
                                                             File.Copy(newPath, newPath.Replace(sourceFilePath, destinationPath + "\\" + foldername), true);


                                                         list = list + destinationPath +"\\"+foldername+ Environment.NewLine;
                                                     }
                                                 }



                                             }
                                             PleaseWait.Destroy();
                                         });


                        MessageBox.Show("Folder Has Been Copied to " + list, "Folder Copied");
                    }
                }

                catch (UnauthorizedAccessException uae)
                {
                    MessageBox.Show(uae.ToString());
                }



            }
        }
    }

You wrote you were learning C#. So, forget about parallel execution, because it unnecessarily makes your task more complicated. Instead, start by decomposing your problem into smaller parts. The code you posted is ugly, long, repeats a lot of logic many times, and hence it is and will be hard to read, debug, and maintain.

So, start by writing small functions for individual files. You need to create a set of folders in a destination folder. Hence write a function accepting a list of names and the destination folder. You need to determine the set of folders from a source folder. So write a function which does that. The combine those two functions together. And so on.

You will end up with a much cleaner, modifiable, reusable solution. Then it will be a lot easier to plug in parallel processing. Most likely, this will be for the sake of learning it, because it makes not much sense to parallelize your problem too heavily.

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