C# "There is not enough space on the disk"实际上存在



我有每分钟写入文件的应用程序。有时(每10分钟 - )我会出现错误

磁盘上没有足够的空间

我的应用程序是Windows表单应用程序。我在Google上阅读了很多文章,但没有给我任何解决结果。

例外:

异常投掷:'system.io.ioexception'in mscorlib.dll

我的代码:

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。在程序的最后运行之前,我进行了碎片化。

我应该如何摆脱此错误?

谢谢

编辑:屏幕可在此处提供

edit2:stacktrace

     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()

链接到另一个屏幕

从您的异常堆栈中有趣的是,当您调用"关闭"方法时发生错误,并且内部调用"齐平"。但是,您已经在代码的上一行中成功地称为"齐平"。因此,我希望将存储异常放在明确的" flush()"呼叫上。这引起了对实际错误原因的怀疑。我会尝试做什么:1.将一次性设备包裹在"使用"块中2.不要明确称呼" flush()",因为该方法在处置/关闭期间都会被调用。

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

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

希望这会有所帮助。

相关内容

最新更新