【问题标题】:c# "There is not enough space on the disk" when in fact there isc#“磁盘上没有足够的空间”实际上有
【发布时间】:2017-08-07 08:20:37
【问题描述】:

我有每分钟写入文件的应用程序。有时(每 10 分钟 +-)我得到错误

磁盘空间不足

我的应用程序是一个 Windows 窗体应用程序。我在谷歌上阅读了很多文章,但没有给出任何解决方法。

例外:

抛出异常:mscorlib.dll 中的“System.IO.IOException”

我的代码:

try
{
    FileStream stream = new FileStream(file, FileMode.CreateNew);
    FileStream stream2 = new FileStream(file2, FileMode.CreateNew);
    BinaryFormatter writer = new BinaryFormatter();

    writer.Serialize(stream, GetProducts().Take(80000).ToList());
    writer.Serialize(stream2, GetProducts().Skip(80000).ToList());
    stream.Flush();
    stream.Close();
    stream2.Flush();
    stream2.Close();
}
catch(Exception ex)
{
    Debug.WriteLine($"FAIL to write: {i} - {ex.Message}");
}

我在磁盘上的总可用空间是 74GB。在上次运行程序之前,我进行了碎片整理。

我应该如何摆脱这个错误?

谢谢

编辑: Screen available here

EDIT2:堆栈跟踪

     at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
     at System.IO.FileStream.WriteCore(Byte[] buffer, Int32 offset, Int32 count)
     at System.IO.FileStream.FlushWrite(Boolean calledFromFinalizer)
     at System.IO.FileStream.Dispose(Boolean disposing)
     at System.IO.FileStream.Finalize()

link to another screen

【问题讨论】:

  • 你想写多少?
  • 你想写到什么位置?
  • 如果您认为这是不应该发生的事情,您可以尝试实施 retry 策略。并且不要处理所有异常,有办法确定when there is no more space
  • 你能显示堆栈跟踪吗?
  • 另外,您的 Temp 文件夹中有多少空间(配额)?我不确定 BinFormatter 是如何工作的,但它的一些亲属会产生临时文件。

标签: c# filestream diskspace


【解决方案1】:

您的异常堆栈中有趣的是,当您调用“Close”方法时会发生错误,并且它会在内部调用“Flush”。但是,您已经成功地在代码的上一行显式调用了“Flush”。所以我希望在显式的“Flush()”调用上引发存储异常。 这引起了对实际错误原因的怀疑。我会尝试做什么: 1.将一次性用品包裹在“使用”块中 2. 不要显式调用“Flush()”,因为在 Dispose/Close 期间无论如何都会调用该方法。

如果仍然失败,请尝试在写入数据之前记录您尝试写入的驱动器的当前可用空间。以下方法将帮助您:

    private static long GetAvailableSpace(string path)
    {
        string drive = Path.GetPathRoot(Path.GetFullPath(path));
        DriveInfo driveInfo = new DriveInfo(drive);
        return driveInfo.AvailableFreeSpace;
    }

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2017-03-31
    • 2014-09-27
    • 1970-01-01
    • 2020-07-13
    • 2012-11-15
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多