2016-12-29 122 views
1

我使用離子拉鍊拉上特定文件夾排除在備份文件夾(創建備份雲)的所有文件夾。Ionic.Zip的ArgumentException(具有相同鍵的項已被添加)

這是我的代碼:

ZipFile zip = new ZipFile(); 
string mainpath = HttpContext.Current.Server.MapPath("~/"); 
Directory.GetDirectories(mainpath).Where(d=> !d.ToLower().EndsWith("backup")).ToList() 
.ForEach(d=> zip.AddDirectory(d)); 

,但增加了一些目錄後,我發現了以下錯誤:

An item with the same key has already been added.

這怎麼可能?如何可能在同一父文件夾中的文件夾列表中重複名稱?

回答

1

當我檢查了例外,它似乎將所有的文件列表中的字典進行歸檔。

我不知道是什麼,它使用作爲可能導致此錯誤的關鍵(可能會使用文件名作爲重點,並在兩個不同的文件夾具有相同的名稱可能會導致它)。

解決方案: 但是我發現AddDirectoryAddFiles有接受存檔目錄路徑中的另一個超載。爲每個目錄提供一個獨特的檔案路徑解決了這個問題。在我來說,我使用:

string mainpath = HttpContext.Current.Server.MapPath("~/"); 
    Directory.GetDirectories(mainpath).Where(d=> !d.ToLower().EndsWith("backup")).ToList() 
.ForEach(d=> zip.AddDirectory(d, d.Substring(mainpath.Length))); 
相關問題