Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to an open Filestream if it is not close due to program crash?

Tags:

c#

wpf

.net-4.5

What will happen to an open FileStream if it is not close due to a program crash? Will it be close automatically? I can't enclose it in a try catch or using because I am doing something with the file. It's like I open the file with an Open button.

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

Then I do something with it. Then in the Exit or closing of the application I execute close.

fs.Close();

But what if something happens in between, like a program crash. Will the file be not accessible since it is not close properly? Thanks.

like image 469
rpb Avatar asked Oct 21 '25 11:10

rpb


1 Answers

Actually we cannot know what will happen in such a situation. MSDN says about the FileStream Class that

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

like image 120
t3chb0t Avatar answered Oct 23 '25 01:10

t3chb0t