Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9203443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:28:30+00:00 2026-06-17T23:28:30+00:00

Let us say that i am writing data to a file handle: hFile =

  • 0

Let us say that i am writing data to a file handle:

hFile = CreateFile(filename, GENERICREAD | GENERICWRITE, 0, null, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
//[snip error check]
try
{
   if (!WriteFile(hFile, buffer, count, ref bytesWritten, null))
       throw new Win32Exception(GetLastError());
}
finally
{
   CloseHandle();
}

If my write of data failed, i want the file to be deleted when i close the handle. I.e.: i want the file to be “un-CreatFile’d”.


i’ve tried the obvious, delete the file if there was a problem:

hFile = CreateFile(filename, GENERICREAD | GENERICWRITE, 0, null, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
//[snip error check]
try
{
   try
   {
      if (!WriteFile(hFile, buffer, count, ref bytesWritten, null))
          throw new Win32Exception(GetLastError());
   }
   finally
   {
      CloseHandle();
   }
}
catch
{
   DeleteFile(filename);
   throw;
}

There are two problems with that approach:

  1. There’s a race condition, where someone could open my file after i close it, but before i delete it
  2. i might have permission to Create a file, but not the Delete it.

What i would like is a way to retroactively specify:

FILE_FLAG_DELETE_ON_CLOSE: The file is to be deleted immediately after all of its handles are closed, which includes the specified handle and any other open or duplicated handles.

to the file that i have open.

i created the file! Surely i can uncreate it! All because i forgot to specify a flag before-hand?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:28:31+00:00Added an answer on June 17, 2026 at 11:28 pm

    In all versions of Windows, you can

    1. Specify FILE_SHARE_DELETE when opening the file originally.
    2. Open it a second time, specifying FILE_FLAG_DELETE_ON_CLOSE
    3. Close the second handle.
    4. The file will be deleted when all handles are closed.

    This is explained in the FILE_FLAG_DELETE_ON_CLOSE description in the documentation for CreateFile:

    The file is to be deleted immediately after all of its handles are closed, which includes the specified handle and any other open or duplicated handles.
    If there are existing open handles to a file, the call fails unless they were all opened with the FILE_SHARE_DELETE share mode.


    If the automatic deletion isn’t good enough, you could look into Transactional NTFS, putting file creation and all writes into a transaction, and then rolling it back if necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem: Let's say there is an XML file that contains both the data
That is to say, let's say I'm writing something that's hosted on foo.com .
Let's say, for illustration, I was writing an iTunes clone app in Cocoa, that
Let's say that I have something like this in my web.xml file. <filter name=foo>
Let's say I'm writing my own function that takes in a CFDataRef object, does
I am writing a iPhone app that calls a web service. Let say the
Let's say I'm writing a little library in C -- some data structure, say.
Question context: let say that there is some really important row in config/locales/en.yml that
Let's say that I have a SQLite database that I create in a separate
Let's say that I have an error in a php/mysql query : $query =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.