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 6214767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:56:06+00:00 2026-05-24T06:56:06+00:00

My windows program receives information from another program via directory/file interface. That is the

  • 0

My windows program receives information from another program via directory/file interface.
That is the other program drops files into a special directory. My program periodically scans the directory, finds the files, processes and then deletes them.

I use CreateFile() function to open such files. To ensure that the other program has finished writing to the file and closed it, I set the dwShareMode parameter to 0. If CreateFile fails with a sharing error I just skip the file until the next itteration.

The problem is that DeleteFile() fails with the ERROR_SHARING_VIOLATION error while the file is opened by my program.

I could close the file before deleteing it, but I would like to avoid the possibility of some other program opening the file just before I delete the file.

I use this code to open files

CreateFile(filePath,DELETE|FILE_READ_DATA,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)

Is it possible to achieve what I want: open the file exclusively an then delete it, so that no other program can interfere between openning and deleteting the file.

  • 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-05-24T06:56:08+00:00Added an answer on May 24, 2026 at 6:56 am

    Pass in FILE_SHARE_DELETE for dwShareMode. Note that this will still allow other applications (as well as your own) to call DeleteFile() while you are reading the file, but according to the documentation of DeleteFile() it won’t be deleted until you call CloseHandle() to close your read handle.

    The DeleteFile function marks a file for deletion on close. Therefore,
    the file deletion does not occur until the last handle to the file is
    closed. Subsequent calls to CreateFile to open the file fail with
    ERROR_ACCESS_DENIED.

    Other applications will not be able to read or write the file as long as you don’t specify FILE_SHARE_READ or FILE_SHARE_WRITE respectively. Although with FILE_SHARE_DELETE then can move the file, but that would be it.

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

Sidebar

Related Questions

I have a c++/windows program that receives data from another c++ program via a
I have a Delphi 6 program that receives audio from an external program via
I have a Windows C++ program which receives and saves bitmap files (*.bmp). My
I wrote some program which uses information about (reads via Windows) hardware of the
I'm porting a windows program from 32 -> 64 bit. It has a 32
I was recently working on a windows program that would sometimes become unresponsive when
I have a program with several worker threads, and a main thread that receives
I have a Windows program that calling a WCF service. This service after a
I have a program that receives input arguments: $ myProgram.exe -arg1 -arg2 -arg3 in
I have a program that passes command-line arguments to an associated file (i.e. associated

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.