I need to create the temp file while I do some work and to delete the file automatically when the work will be finished. This file contents some info which is for reading by other applications and becomes unnecessary when the work is completed. I try to use FileShare.Read
and FileOptions.DeleteOnClose
but at this case other applications can't read the file.
var documentName = "some-document";
var editor = "Bob";
using (var fs = new FileStream($"{documentName}.log", FileMode.Create,
FileAccess.Write, FileShare.Read, 4 * 1024, FileOptions.DeleteOnClose))
{
string info = $"{DateTime.Now}: The '{documentName}.xml' file " +
$"is edited by {editor} right now.";
var data = Encoding.UTF8.GetBytes(info);
fs.Write(data, 0, data.Length);
// Here is some big work with some-document.xml file
#if DEBUG
fs.Flush();
#endif
} // <-- I set the breakpoint here in IDE and try to open
// the some-document.log file by Notepad++.
// But Notepad++ can't to do it.
If I comment the , 4 * 1024, FileOptions.DeleteOnClose
code chunk then other applications can read my some-document.log
file. But at this case I am to write additional code chunk for delete my temp file (I would like to awoid it...).
Why FileShare.Read
doesn't work in combination with FileOptions.DeleteOnClose
?