-1

嗨,我正在尝试编写一个简单的程序来将一个文件夹从一个源复制到多个并行。我正在学习 c#,所以一直在尝试理解和更改代码示例,因为我认为这是学习新事物的最佳方式。

下面的示例不起作用,因为它仅复制到destinationPaths 中的第一个目标

奇怪的是我有一种类似的方法可以将一个文件复制到多个文件,而且每次我错过了什么都有效?如果有人能告诉我为什么这不起作用我会很感激我猜可能有些事情你不能同时做

任何建议都会很棒

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());
                }



            }
        }
    }
4

1 回答 1

3

你写道你正在学习 C#。因此,忘记并行执行,因为它不必要地使您的任务更加复杂。相反,首先将您的问题分解成更小的部分。您发布的代码丑陋、冗长,多次重复大量逻辑,因此很难阅读、调试和维护。

因此,首先为单个文件编写小函数。您需要在目标文件夹中创建一组文件夹。因此编写一个接受名称列表和目标文件夹的函数。您需要从源文件夹中确定文件夹集。所以写一个函数来做到这一点。将这两个功能结合在一起。等等。

你最终会得到一个更干净、可修改、可重复使用的解决方案。然后插入并行处理会容易得多。最有可能的是,这将是为了学习它,因为过多地并行化您的问题没有多大意义。

于 2012-12-08T12:48:53.820 回答