我有一个执行模拟并不断读取/写入二进制文件的表单应用程序。如果您让它运行,一切都会正常工作。但是,如果表单已关闭/模拟被中止,则文件流未正确关闭 - 使文件处于锁定状态。有没有办法确保所有流都关闭?我尝试了以下 - 但它没有效果......提前非常感谢,T
public BinaryWriter BinWrite;
public BinaryReader BinRead;
public BinaryWriter EnvBinWrite;
public BinaryReader EnvBinRead;
public void theForm_FormClosing(object sender, FormClosingEventArgs e)
{
//Close all binary file reader/writers -- crashes if it cannot overwrite files
foreach (Building B in AllBldgs)
{
try
{
EnvBinRead.Close();
}
catch
{ continue; }
try
{
EnvBinWrite.Close();
}
catch
{ continue; }
try
{
BinRead.Close();
}
catch
{ continue; }
try
{
BinWrite.Close();
}
catch
{ continue; }
}
}