C# 值不能为null。参数名称:dest

C# 值不能为null。参数名称:dest,c#,.net,zipfile,streamwriter,C#,.net,Zipfile,Streamwriter,我有一个小应用程序,可以将一些文本数据写入压缩文本文件。它在第一次存档时工作正常,但一段时间后,我出现以下未处理的异常: System.ArgumentNullException: Value cannot be null. Parameter name: dest 我添加了&&line.Lenght>0,因为我认为这可能是问题所在,但没有任何改变。 我在3小时内得到了1个完整的归档文件,有时是第二个归档文件的一半,这个文件更小,我想它还没有完成,而是以.tmp扩展名写入磁盘。我可以将其重命名

我有一个小应用程序,可以将一些文本数据写入压缩文本文件。它在第一次存档时工作正常,但一段时间后,我出现以下未处理的异常:

System.ArgumentNullException: Value cannot be null.
Parameter name: dest
我添加了
&&line.Lenght>0
,因为我认为这可能是问题所在,但没有任何改变。 我在3小时内得到了1个完整的归档文件,有时是第二个归档文件的一半,这个文件更小,我想它还没有完成,而是以.tmp扩展名写入磁盘。我可以将其重命名为zip并打开/解包/读取数据。 如果我将“计数器”更改为较小的时间段,例如10分钟,它会写入一堆档案,就像一切正常一样。足以让我觉得虫子不见了。 所以这个错误可能会在大约4-5小时后出现。 如何解决这个问题

堆栈跟踪:

[ERROR] FATAL UNHANDLED EXCEPTION: 
Unhandled Exception:
System.ArgumentNullException: Value cannot be null.
Parameter name: dest
  at System.IO.MemoryStream.set_Capacity (System.Int32 value)
  at System.IO.MemoryStream.EnsureCapacity (System.Int32 value)
  at System.IO.MemoryStream.Write (System.Byte[] buffer, System.Int32 offset, System.Int32 count)
  at SharpCompress.IO.NonDisposingStream.Write (System.Byte[] buffer, System.Int32 offset, System.Int32 count)
  at System.IO.Compression.ZipArchiveEntryStream.Write (System.Byte[] buffer, System.Int32 offset, System.Int32 count)
  at System.IO.StreamWriter.Flush (System.Boolean flushStream, System.Boolean flushEncoder)
  at System.IO.StreamWriter.Write (System.Char[] buffer, System.Int32 index, System.Int32 count)
  at System.IO.TextWriter.WriteLine (System.String value)
  at Program.DataWriter ()
  at System.Threading.ThreadHelper.ThreadStart_Context (System.Object state) 
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx)
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx)
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state)
  at System.Threading.ThreadHelper.ThreadStart () 
代码如下:

static ConcurrentQueue<string> queue = new ConcurrentQueue<string>();

static void Main(string[] args)
{
    Thread thread = new Thread(DataWriter);
    thread.Start();
}

static void DataWriter()
{
    while (true)
    {
        string start = DateTime.Now.ToString("yy-MM-dd-HH-mm-ss");
        DateTime counter = DateTime.Now.AddHours(3);

        using (var archive = ZipFile.Open(HistoryPath + "history-" + start + ".tmp", ZipArchiveMode.Update))
        {
            ZipArchiveEntry data = archive.CreateEntry("data-" + start + ".txt");
            using (StreamWriter writer = new StreamWriter(data.Open()))
            {
                while (DateTime.Now < counter)
                {
                    if (queue.TryDequeue(out string line))
                    {
                        if (line != null && line.Length > 0)
                        {
                            writer.WriteLine(line);
                        }
                    }

                    System.Threading.Thread.Sleep(10);
                }
            }
        }

        File.Move(HistoryPath + "history-" + start + ".tmp", HistoryPath + "history-" + start + "_"+ DateTime.Now.ToString("yy-MM-dd-HH-mm-ss") + ".zip");
    }
}
静态ConcurrentQueue队列=新建ConcurrentQueue();
静态void Main(字符串[]参数)
{
线程线程=新线程(DataWriter);
thread.Start();
}
静态void DataWriter()
{
while(true)
{
字符串start=DateTime.Now.ToString(“yy-MM-dd-HH-MM-ss”);
DateTime计数器=DateTime.Now.AddHours(3);
使用(var archive=ZipFile.Open(HistoryPath+“history-”+start+“.tmp”,ZipArchiveMode.Update))
{
ZipArchiveEntry data=archive.CreateEntry(“数据-”+start+“.txt”);
使用(StreamWriter=newstreamwriter(data.Open()))
{
while(DateTime.Now<计数器)
{
if(queue.TryDequeue(输出字符串行))
{
if(line!=null&&line.Length>0)
{
writer.WriteLine(行);
}
}
系统线程线程睡眠(10);
}
}
}
文件.Move(HistoryPath+“history-”+start+“.tmp”,HistoryPath+“history-”+start+““+DateTime.Now.ToString(“yy-MM-dd-HH-MM-ss”)+“.zip”);
}
}

更改ZIP库解决了我的问题。我已将Microsoft System.IO.Compression更改为。代码几乎相同。现在一切正常。Microsoft库中似乎有错误。

FYI
if(line!=null&&line.Length>0)
->
if(!string.IsNullOrEmpty(line))
。。。。你实际上想做什么,因为这可能是错误的(或可疑的)approach@TheGeneral我从不同的线程收集字符串行。我希望在将它们添加到队列时,将它们逐行写入zip文件中。我想把它分割成n个小时的周期档案。@一般来说,我知道这段代码可以被优化一点,比如移动
计数器
启动
otside循环。我试着检查是什么导致了这个错误。。这很有趣,因为这个异常是从那里的内存流引发的。。。我不认为您的代码示例真的会导致异常。。这似乎与正在丢弃的流本身有关。可能是
ZipArchiveEntry
的流处理。。这里有一个关于单独产品的类似问题,但没有太多帮助。