11

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.

David Basarab
  • 72,212
  • 42
  • 129
  • 156
Anuraj
  • 18,859
  • 7
  • 53
  • 79

3 Answers3

11

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.

Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • But I am getting an exception "IO operation will not work. Most likely the file will become too long or the handle was not opened to support synchronous IO operations.", if I put | instead of &, when I am calling this.fcommandHandler.Close() – Anuraj Sep 18 '09 at 12:33
  • 2
    @Anuraj: That doesn't mean | is wrong, it means something in your usage is incorrect. Lasse is right that you need | instead of &. The exception is your next problem to solve. – Jeff Yates Sep 18 '09 at 13:23
  • @Jeff Yates: Thanks let my try with some other options. – Anuraj Sep 18 '09 at 14:20
  • How do I do this in VB.NET? I am having a similar problem with `And` the result is 0 – HackSlash Jun 20 '18 at 22:39
  • Try using `Or` instead, I'm not a VB expert though so if you're stuck you might want to search for it. – Lasse V. Karlsen Jun 21 '18 at 06:28
4

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

FILE_FLAG_NO_BUFFERING & FileOptions.DeleteOnClose returns FileOptions.None

Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
  • Please check the comment of the first answer. – Anuraj Sep 18 '09 at 12:34
  • @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 Sep 18 '09 at 12:39
  • @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 Sep 18 '09 at 12:51
  • @Anuraj: Thank you. You can see the | being applied in the answers on that thread. – Yuriy Faktorovich Sep 18 '09 at 12:59
  • @Yuriy Faktorovich:I am getting an exception "IO operation will not work. Most likely the file will become too long or the handle was not opened to support synchronous IO operations.", if I put | instead of &, when I am calling this.fcommandHandler.Close() – Anuraj Sep 18 '09 at 13:14
0

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?

Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • @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 Sep 18 '09 at 13:05
  • 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 Sep 18 '09 at 13:31