gpt4 book ai didi

c# - 如果由于程序崩溃而未关闭,打开的文件流会发生什么情况?

转载 作者:太空宇宙 更新时间:2023-11-03 10:59:27 26 4
gpt4 key购买 nike

如果由于程序崩溃而没有关闭打开的 FileStream 会发生什么?会自动关闭吗?我无法将其包含在 try catch 或 using 中,因为我正在对文件执行某些操作。就像我使用“打开”按钮打开文件一样。

private void button1_Click(object sender, RoutedEventArgs e)
{
fs = new FileStream("Test.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
}

然后我用它做点什么。然后在退出或关闭应用程序时执行关闭。

fs.Close();

但是如果中间发生了什么,比如程序崩溃怎么办。该文件是否会因为未正确关闭而无法访问?谢谢。

最佳答案

实际上我们无法知道在这种情况下会发生什么。 MSDN 说关于 FileStream Class那个

If a process terminates with part of a file locked or closes a file that has outstanding locks, the behavior is undefined.

关于c# - 如果由于程序崩溃而未关闭,打开的文件流会发生什么情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18222131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com