2012-12-08 59 views
-1

嗨我想寫一個簡單的程序來將文件夾從一個soure複製到多個並行。 我正在學習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()); 
       } 



      } 
     } 
    } 
+6

你可以發佈更簡潔的代碼示例嗎?這是很多代碼讀取... –

+0

爲什麼你想並行複製文件?任務是IO綁定。我沒有看到任何好處。 –

+0

您可能在循環內的代碼中遇到異常。我敢打賭,如果你捕獲了結果並檢查了結果中的'LowestBreakIteration',你會發現它只迭代了第一個索引。 –

回答

3

你寫你學習C#。所以,忘記並行執行,因爲它不必要地讓你的任務更復雜。相反,首先將問題分解爲更小的部分。您發佈的代碼很難看,很長時間,重複了很多次邏輯,因此它很難讀取,調試和維護。

因此,首先編寫單個文件的小函數。您需要在目標文件夾中創建一組文件夾。因此編寫一個接受名稱列表和目標文件夾的函數。您需要從源文件夾中確定一組文件夾。所以寫一個這樣做的函數。這兩個功能結合在一起。等等。

您將得到一個更清潔,可修改,可重用的解決方案。然後插入並行處理將會變得更容易。最有可能的是,這是爲了學習它,因爲並行化你的問題太沒有意義了。

+0

優秀的我會嘗試我沒有想過要把它分解下來 –

+0

感謝您的建議我已經看到了將它分解出來的錯誤,它歸結爲以第一次刪除\ *結尾的源文件if語句。 *第二次沒有*,所以停止。但分解確實使它更容易理解,所以感謝您的建議 –

+0

太棒了,我很高興它有幫助。 –

相關問題