views:

516

answers:

3

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.

+4  A: 

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.

Lasse V. Karlsen
Anuraj
Jeff Yates
@Jeff Yates: Thanks let my try with some other options.
Anuraj
+2  A: 

Use FileOptions.DeleteOnClose | FILE_FLAG_NO_BUFFERING the & cancels them out.

FILE_FLAG_NO_BUFFERING & FileOptions.DeleteOnClose returns FileOptions.None

Yuriy Faktorovich
Please check the comment of the first answer.
Anuraj
@Anuraj: I'm trying to figure out what the File_flag_no_buffering is, the DeleteOnClose is definitely not getting called because you're doing an and instead of an or.
Yuriy Faktorovich
@Yuriy Faktorovich - The File_flag_no_buffering file option is a file option, which helps to avoid file caching when reading and writing files, by Windows. Check this http://stackoverflow.com/questions/122362/how-to-empty-flush-windows-read-disk-cache-in-c
Anuraj
@Anuraj: Thank you. You can see the | being applied in the answers on that thread.
Yuriy Faktorovich
Anuraj
A: 

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?

AnthonyWJones
@AnthonyWJones: I tried this but still I am getting this error. I am trying achieve is like : I have to issue some commands to a USB via a File(ex. cmd.bin) device, and it will return some responses in a File(ex. res.bin). But if I use normal C# Stream Windows will cache the response and always I will get the same response, so I have to use the No Buffering flag. And the files are temporary so I have to delete it after use;thats why I am using Delete On close flag.
Anuraj
In that case the question that Yuriy has directed to you has a different approach to opening such a file, I'd give that a go.
AnthonyWJones