DeleteFile fails unexpectedly

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi,
We are using this piece of code to delete a file.

HANDLE hfile = NULL;
HRESULT hr = S_OK;
WIN32_FIND_DATA FindFileData;

hfile = FindFirstFile((LPCTSTR)filename, &FindFileData);
if (hfile == INVALID_HANDLE_VALUE)
{
hr = BOERR_FILE_NOT_FOUND ;
}
else
{
bool bRetVal = FindClose(hfile) ;
if(!DeleteFile(filename))
{
CString strError;
strError.Format(_T("CFileReaderWriter::Delete : Could delete an
existing file. HR = %ld"), GetLastError());
AfxMessageBox(strError ) ;
hr = BOERR_FAILED_DELETE_FILES ;
}
}

We make sure that the file exists by calling FindFirstFile(), so if it
tries to delete the file its sure that the file exists. I find that the
error returned from DeleteFile() is '2' which happens to be "The system
cannot find the specified file". Is it possible due to some reason that
this error could come up even though the file is present.

Specific information :
* The deletion happens on a secondary thread.
* The main thread could attempt to read the file in the process while
the secondary file is deleting the file, but I have made sure
that the Critical sections are locked correctly to prevent simultaneous
access.

Also is it possible that DeleteFile could return successfully but the
file is still not deleted?

Regards,
Janakiraman

.



Relevant Pages

  • Re: Strange hint: Inline function DeleteFile has not been expanded??
    ... As soon as I do, I get all sorts of other errors, but without ... You implement your own DeleteFile(), ... You need to typecast string variables holding the file name to PChar when calling DeleteFile() does). ... if you find an error in the source code, ...
    (alt.comp.lang.borland-delphi)
  • Re: File IO and Threading
    ... it is supposed to remove it from the directory tree, ... The method is nothing but a wrapper around the DeleteFile method in Win32 ... The DeleteFile function marks a file for deletion on close. ...
    (microsoft.public.dotnet.framework)
  • How to get the open handles of a file
    ... using DeleteFile(). ... According to the documentation of DeleteFile,DeleteFilemarks a ... file for deletion on close and the file is deleted when the last ... Rohini Chandra ...
    (microsoft.public.vc.language)