Recent

Author Topic: [SOLVED] DeleteFile not working on Windows 11  (Read 1903 times)

jamestien

  • New Member
  • *
  • Posts: 48
[SOLVED] DeleteFile not working on Windows 11
« on: January 15, 2022, 02:27:12 pm »
Hello,

Just updated to Windows 11, which caused my program stopped working properly. My program creates temporary file on startup and will delete it upon program termination. Found out that "DeleteFile" function will always return Access denied and failed for deletion. All the file security properties were set to full control for all the accounts.

The temp filename was randomly generated based on current time.
cidbaf63589d72ba91f3c050aa399035a9f6


Anyone encounter the same issue on Windows 11?


UPDATE:
Found the culprit, temporary file created by Zipper had READONLY attribute.

Code: Pascal  [Select][+][-]
  1.   unzip := TUnZipper.Create;
  2.   try
  3.     unzip.FileName := sFileName;                                     //Input of compressed ZIP file
  4.     unzip.OutputPath := GetTempDir(FALSE);                           //Output directory for unzipped files.
  5.     unzip.Examine;
  6.     unzip.UnZipAllFiles;
  7.     RenameFile(unzip.OutputPath+unzip.Entries[0].ArchiveFileName,gDatabase_TempFile);
  8.    
  9.     FileSetAttr(gDatabase_TempFile,faNormal);  //<============================= This solved the issue.
  10.  
  11.     //DeleteFile(gDatabase_TempFile);
  12.  
  13.   finally
  14.     unzip.Free;
  15.   end;
  16.  
  17.  
« Last Edit: January 15, 2022, 04:21:37 pm by jamestien »
Lazarus 2.2.0 (Win11, Xubuntu, macOS Big Surr)

GetMem

  • Hero Member
  • *****
  • Posts: 3741
Re: DeleteFile not working on Windows 11
« Reply #1 on: January 15, 2022, 02:44:05 pm »
@jamestien
Quote
Anyone encounter the same issue on Windows 11?
No, but what error message do you get?
Code: Pascal  [Select][+][-]
  1. if not DeleteFile(Path) then
  2.   ShowMessage(SysErrorMessage(GetLastOSError));

jamestien

  • New Member
  • *
  • Posts: 48
Re: DeleteFile not working on Windows 11
« Reply #2 on: January 15, 2022, 02:49:44 pm »
GetLastError retuned value of 5, which is Access denied.

The failed filename was "cidbaf63589d72ba91f3c050aa399035a9f6" or any other random strings. Tried to delete other more standard 8.3 filename without problem, really weird on Win 11, never had problem on Win10 before.
Lazarus 2.2.0 (Win11, Xubuntu, macOS Big Surr)

GetMem

  • Hero Member
  • *****
  • Posts: 3741
Re: DeleteFile not working on Windows 11
« Reply #3 on: January 15, 2022, 03:21:19 pm »
@jamestein
Strange. If you have full access to file and the file is not locked by another program(or yours), like this:
Code: Pascal  [Select][+][-]
  1. var
  2.   fs: TFileStream;
  3. begin
  4.   fs := TFileStream.Create('Path', fmOpen or fmShareDenyWrite);
  5.   try
  6.     //access denied here
  7.   finally
  8.     fs.Free; //you forget to free the stream
  9.   end;
  10. end;
then the only remaining option is an antiviurs software.

Also I found this: https://forum.lazarus.freepascal.org/index.php/topic,48284.msg347662.html#msg347662 I'm not sure if it helps.

af0815

  • Hero Member
  • *****
  • Posts: 936
Re: DeleteFile not working on Windows 11
« Reply #4 on: January 15, 2022, 03:26:53 pm »
If times goes by the file deleteable ? I have seen on windows two problems with deleting of files.

The first issue, if a AV-Scanner will hold the file for upto 20 second blocked (= access denied on my side)
The second, if if have graphical data inside and this on a gui element, the file will also blocked in the lifetime of the image. A copy of the file will not unlock this issue. The lock is only released, if another from a other file is loaded, or the application is closed.

I have such issues with generating and showing eg. self generated preview pictures.
regards
Andreas

jamestien

  • New Member
  • *
  • Posts: 48
Re: DeleteFile not working on Windows 11
« Reply #5 on: January 15, 2022, 03:49:47 pm »
Thank you all for the reply. here is my findings so far, and I think its SQLite3 database related?

The file that can't be deleted is a SQLite3 database. It is designed to be Unzipped to temporary folder with random filename during program start up, then Compressed and saved to a proper name (old one gets deleted) upon program exit. It was all running fine on Windows 10, Linux, MacOS. but only failed in Windows 11, the error was caused by the file deletion routine, and database was also read-only on Win11.

- The file can be delete in Windows, no specially permision needed. just can't be deleted from code. (Lazarus, Delphi)
- The database file was opened READ-ONLY in Windows 11, where I already specify it to be Read/Write mode.
- Tried disable MS Defender, my program still can't delete the temporary Sqlite3 database file it created during startup.
- Tried create an empty file with the same filename (can't be deleted), the function succeed.

Weird... really regret on this Windows 11 upgrade. hahaha

Code: Pascal  [Select][+][-]
  1. //[Cleanup routine]
  2.  
  3.   SQLTransaction1.Active := FALSE;
  4.   SQLite3Connection1.Close;
  5.  
  6.   zip := TZipper.Create;
  7.  
  8.   try
  9.     zip.FileName := 'Sample.xxx';               //Output file name of compressed Database file
  10.     zip.Entries.AddFileEntry(gDatabase_TempFile,'Database');   //131b9eef3d980273c3b609c36b648a33
  11.     zip.ZipAllFiles;
  12.     Sleep(500);
  13.  
  14.     DeleteFile(gDatabase_TempFile);  //131b9eef3d980273c3b609c36b648a33
  15.  
  16.   finally
  17.     zip.Free;
  18.   end;
  19.  
  20.  
« Last Edit: January 15, 2022, 03:57:05 pm by jamestien »
Lazarus 2.2.0 (Win11, Xubuntu, macOS Big Surr)

winni

  • Hero Member
  • *****
  • Posts: 3045
Re: DeleteFile not working on Windows 11
« Reply #6 on: January 15, 2022, 03:53:36 pm »
Hi!

Disable your AntiVirusSoftware and look what's gonna happen.

Winni

jamestien

  • New Member
  • *
  • Posts: 48
Re: DeleteFile not working on Windows 11
« Reply #7 on: January 15, 2022, 03:54:48 pm »
Hi!

Disable your AntiVirusSoftware and look what's gonna happen.

Winni

Thank you winni, I did it but no luck.
Lazarus 2.2.0 (Win11, Xubuntu, macOS Big Surr)

GetMem

  • Hero Member
  • *****
  • Posts: 3741
Re: DeleteFile not working on Windows 11
« Reply #8 on: January 15, 2022, 04:16:51 pm »
@jamestein

Move the DeleteFile(gDatabase_TempFile) outside the try finally block. 

Thaddy

  • Hero Member
  • *****
  • Posts: 11517
Re: DeleteFile not working on Windows 11
« Reply #9 on: January 15, 2022, 04:18:09 pm »
Hi!

Disable your AntiVirusSoftware and look what's gonna happen.

Winni

Thank you winni, I did it but no luck.
Three other options
- make sure the file is really closed.
- use a utility like whoislocking to check if your own program holds a lock. https://sourceforge.net/projects/whoislocking/
- check the permissions on the file. That becomes more and more strict.
Путин преступник. Россияне дезинформированы.

jamestien

  • New Member
  • *
  • Posts: 48
Re: [SOLVED] DeleteFile not working on Windows 11
« Reply #10 on: January 15, 2022, 04:23:52 pm »
UPDATE:
Found the culprit, temporary file created by Zipper had READONLY attribute.


Thank you all for the help and suggestion. It was the Zipper component caused the issue. Removed the read only file attribute solved problem.
Lazarus 2.2.0 (Win11, Xubuntu, macOS Big Surr)

GetMem

  • Hero Member
  • *****
  • Posts: 3741
Re: [SOLVED] DeleteFile not working on Windows 11
« Reply #11 on: January 15, 2022, 04:30:34 pm »
@jamestien
Quote
Just updated to Windows 11
and
Quote
never had problem on Win10 before.
and
Quote
Found the culprit, temporary file created by Zipper had READONLY attribute.
I'm glad it's working, but wasn't the temporary file readonly on win10 too? :) What changed with TZipper between win10 and win11?
« Last Edit: January 15, 2022, 04:33:52 pm by GetMem »

jamestien

  • New Member
  • *
  • Posts: 48
Re: [SOLVED] DeleteFile not working on Windows 11
« Reply #12 on: January 15, 2022, 04:38:51 pm »
@jamestien
Quote
Just updated to Windows 11
and
Quote
never had problem on Win10 before.
and
Quote
Found the culprit, temporary file created by Zipper had READONLY attribute.
I'm glad it's working, but wasn't the temporary file readonly on win10 too? :) What changed with TZipper between win10 and win11?

I don't know GetMem, the program is the inventory system for my own business, I use it to encode sales everyday. It just pop up error and crash on fresh installed Windows 11. It never had problem before when I was on Windows 10, after adding the code to remove the read only attribute it is working now on Windows 11.
« Last Edit: January 15, 2022, 04:43:14 pm by jamestien »
Lazarus 2.2.0 (Win11, Xubuntu, macOS Big Surr)

 

TinyPortal © 2005-2018