使用memoryStream时,已经添加了具有相同键的项

时间:2019-11-13 08:03:45

标签: c# zip memorystream

我正在使用DotNetZip,其想法是将zip所有文档添加到存档中,然后将其保存在桌面上。

  var outputdirectory = Path.Combine(Settings.Default.InvoiceXmlFolder, "InvoicesSigned.zip");

        using (ZipFile zip = new ZipFile(outputdirectory))
        {
            var count = 0;
                foreach (var document in documents)
                {
                    count++;
                    try
                    {
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            document.Save(memoryStream);

                            memoryStream.Seek(0, SeekOrigin.Begin);

                            count++;
                            zip.AddEntry($"{count}.xml", memoryStream);
                            zip.Save();
                            resultValue = InvoiceResult.Success;
                        }
                    }
                    catch (IOException)
                    {
                        resultValue = InvoiceResult.CannotSendZipFile;
                   //     throw new IOException($"Zip file in path {outputdirectory} has already been created. Please delete file if you want to make a new one.");
                    }
                }
}

我以为这是计数,所以我在foreach中将其增加了,但仍然相同。

第一次尝试zip.addEntry时,它将在Memory流上引发异常。

  

System.ArgumentException:'具有相同键的项已经被   已添加。”

0 个答案:

没有答案
相关问题