只会复制数组c#中的第一项

时间:2012-12-08 12:34:07

标签: c#

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

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

stange的事情是我有一个simlar方法将一个文件复制到许多文件,这每次都有效 我错过了什么?如果有人能告诉我为什么这不起作用我会很高兴我猜你可能有某些事情你不能并行做什么

任何建议都会很棒

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



            }
        }
    }

1 个答案:

答案 0 :(得分:3)

你写道你正在学习C#。所以,忘记并行执行,因为它不必要地使你的任务更复杂。相反,首先将问题分解为更小的部分。你发布的代码是丑陋的,很长一段时间,多次重复很多逻辑,因此很难读取,调试和维护。

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

您将获得更清洁,可修改,可重复使用的解决方案。然后插入并行处理会容易得多。最有可能的是,这将是为了学习它,因为将问题并行化得太重要了。