Forum > Windows

Deleting exe-file not working anymore in Windows 11

(1/2) > >>

JanRoza:
I have a program that has been doing its job perfectly under Windows until I migrated to Windows 11.
It is a small secondary program that checks if the main program has been closed and then renames the old main program and after that renames the new downloaded program file to the original programs name. This has been working under all versions of Windows but fails to delete the old main program in Windows 11.
Has anyone experienced the same or know how to improve the code I use for this operation?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  // Wait until GliderMaintenance is closed  while (GmIsRunning(strProgram) <> 0) do bActive := True;  // GliderMaintenance.exe   // Delete archived previous version  DeleteFile(PChar(strOldProgram));    // GliderMaintenance-old.exe     «-------- This fails in Windows 11   // Rename current main program to old and after that rename new main version to current name  if RenameFile(strProgram, strOldProgram)             // GliderMaintenance.exe -> GliderMaintenance-old.exe  then RenameFile(strNewProgram, strProgram);     // GliderMaintenance.new -> GliderMaintenance.exe   // Start new version of GliderMaintenance  ShellExecute(Handle, 'open', PChar(strProgram), nil, nil, 1);   // Close this update program  Close; 

Wallaby:
Depending on how you check if it's running, this code is bad for two reasons:

1) Consuming CPU for no reason for repeated checks (can be thousands per second!)
2) A race condition is possible where your code thinks it's not running, but Windows hasn't finishing unloading and releasing the file

Do this instead:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---while FileExists(strOldProgram) and (not DeleteFile(PChar(strOldProgram))) do Sleep(100); // This will not proceed until the program is closed.

marcov:
This is fairly typical, and not windows 11 specific, e.g. an antivirus can keep the file locked. Or a second instance could be launched(e.g. in an elevated context).

Solution is simple, rename to old<x> after a timeout, and check on startup if files need deleting. (could be a problem if it is in an elevated directory though)

JanRoza:
I'm pretty sure that the main program is not running as the 'GmIsRunning(strProgram) <> 0' loop is ended (without any noticable delay). Also this code is running for more than 10 years in all previous versions of Windows and never failed me once.
The problem is that the DeleteFile works in Windows < version 11 and does not work in Windows 11.
I will try Wallaby's suggestion as well and see if it solves my problem, I will let you know the result.

JanRoza:
Guys, thanks for your advice, they were useful.
But the real cause was me myself, I was dumb enough to forget to put the new main program version on the server.  :-[
So the process was still working but it updated the main program with the same version instead of the next version, so right after updating and restarting the main program it again showed the message that there was a new version available and asked me if I wanted to update.
So apologies for my stupidity and I will try to be more awake next time.  >:D

Navigation

[0] Message Index

[#] Next page

Go to full version