In C#, I open a file with FileShare.Delete. This allows me to open the file without restricting other processes from deleting it. For example:
using (FileStream fs = new FileStream(@"C:\temp\1.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
{
int len = (int)fs.Length;
byte[] array = new byte[len];
int bytesRead = fs.Read(array, 0, len);
}
My questions are:
- What happens if the file is deleted by a different process after we created the stream, but before we read it? Does the operating system keep a copy of the file until the stream\handle is closed?
- Can I rely on reading the deleted file without getting any errors, or the wrong content?