Click here to Skip to main content
15,891,747 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having trouble deleting files under XP and Windows 7

unlink and DeleteFile fail under Window 7 with access denied, but Win32DeleteFile() works. Under XP, Win32DeleteFile() fails.

The file is not in use, and does not show up process explorer, and can be deleted always without problems using explorer. It probably has a file handle open, but not exclusive, etc as explorer can delete the file, I just can't in the application.

Any ideas?

Thanks!
Posted

Hi David,

Actually there is some restrictions in using this API. And these restriction are related to OS and the location from which you are deleting the files.

Please refer to the "Requirements" sections of the MSDN link shared below,
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762265(v=vs.85).aspx[^]

And if your OS still satisfy those requirement then do share your piece of code that is trying to delete the file.

FYI: this API is not supported in Windows 7.

Bye.
 
Share this answer
 
What I have discovered is that Win32DeleteFile() works under Win 7, and unlink/DeleteFile works under XP, so I am using one or the other depending.

Thanks!
 
Share this answer
 
Another strategy you can try is to send the file(s) in question to the recycling bin. Here is a variation on some of my code (retyped; apologies if there is a compile error):

C++
bool FileToRecycleBin(const char* szFileIn)
{
  char  szFile[1030];
  SHFILEOPSTRUCT  op = {0};

  strcpy_s(szFile, szFileIn);
  szFile[strlen(szFile)+1] = '\0'; // Extra terminating NUL required...

  op.wFunc = FO_DELETE;
  op.pFrom = szFile;
  op.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT;

  return 0 == SHFileOperation(&op);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900