问题
In my project I have to create some temp files in an USB device, which I want to delete on Closing. So I used a code like
this.fcommandHandler = new FileStream(TempFileName,
FileMode.CreateNew, FileAccess.ReadWrite,
FileShare.ReadWrite, 512, FileOptions.DeleteOnClose);
It works fine. But the problem is I want to use one more FileOption, like No buffering.
private const FileOptions FILE_FLAG_NO_BUFFERING = (FileOptions)0x20000000;
this.fcommandHandler = new FileStream(TempFileName,
FileMode.CreateNew, FileAccess.ReadWrite,
FileShare.ReadWrite, 512, FileOptions.DeleteOnClose & FILE_FLAG_NO_BUFFERING);
But its not deleting the File after closing. Please help.
回答1:
You need to use |
instead of &
.
These are binary flags, and when you say &
, you effectively mask them all away, resulting in no options at all.
回答2:
Use FileOptions.DeleteOnClose | FILE_FLAG_NO_BUFFERING
the &
cancels them out.
FILE_FLAG_NO_BUFFERING & FileOptions.DeleteOnClose
returns FileOptions.None
回答3:
Try including the WriteThrough
flag as well in a list using the | operator. See this KB on the requirements for using FILE_FLAG_NO_BUFFERING
. Its interesting that MS hasn't included this flag in the enum. Is there a reason why WriteThrough
doesn't do what you need in this scenario? You are trying to write secure data?
来源:https://stackoverflow.com/questions/1444262/filestream-with-deleteonclose-file-option