Autor Beitrag
Wolle92
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Mi 05.09.07 20:25 
Hallo,

Mein Programm kriegt es nicht gebacken, eine EXE-Datei zu löschen...
Mach ich irgendwas falsch?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
try
    DeleteFile(ExtractFilePath(Application.ExeName)+'VokabelFit.exe');
    if not FileExists(ExtractFilePath(Application.ExeName)+'VokabelFit.exe') then
    begin
      RenameFile(ExtractFilePath(Application.ExeName)+'VokabelFit2.exe',
        ExtractFilePath(Application.ExeName)+'VokabelFit.exe');
      ShellExecute(handle, nil, PChar(ExtractFilePath(Application.ExeName)+'VokabelFit.exe'),
        PChar('delete'), nil, SW_SHOW);
    end
    else
      ShowMessage('Konnte Vorgang nicht beenden... Abbruch!1'); //<< Hier lande ich immer...
  except
    on EAccessViolation do
      ShowMessage('Konnte Vorgang nicht beenden... Abbruch!2');
  end;


Es ist sicher, dass alle Dateien existieren...
hathor
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mi 05.09.07 20:39 
Das "Problem" kommt alle paar Wochen wieder:

Du kommst mir vor wie einer, der jemanden auf dem Flughafen in den Urlaub verabschiedet und zu Hause gleich in den Briefkasten schaut, ob schon eine Ansichtskarte angekommen ist.

Merkst Du was ???
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Do 06.09.07 14:38 
Wie kann ich das Programm denn warten lassen, bis die Datei gelöscht ist...?
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 06.09.07 14:47 
DeleteFile ist eine Funktion. Das könnte man ja ausnutzen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
try
    if DeleteFile(ExtractFilePath(Application.ExeName)+'VokabelFit.exe') then
      while FileExists(ExtractFilePath(Application.ExeName)+'VokabelFit.exe') do 
        Application.ProcessMessages;
    if not FileExists(ExtractFilePath(Application.ExeName)+'VokabelFit.exe') then
    begin
      RenameFile(ExtractFilePath(Application.ExeName)+'VokabelFit2.exe',
        ExtractFilePath(Application.ExeName)+'VokabelFit.exe');
      ShellExecute(handle, nil, PChar(ExtractFilePath(Application.ExeName)+'VokabelFit.exe'),
        PChar('delete'), nil, SW_SHOW);
    end
    else
      ShowMessage('Konnte Vorgang nicht beenden... Abbruch!1'); //<< Hier lande ich immer...
  except
    on EAccessViolation do
      ShowMessage('Konnte Vorgang nicht beenden... Abbruch!2');
  end;
Ist ein bisschen Quick and Dirty, könnte Deine Probleme aber lösen.
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Do 06.09.07 15:13 
Funktioniert immer noch nicht...
Mit einer Abfrage, bo die Datei existiert gehts auch nicht...
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 06.09.07 15:32 
Ich bin ein Idiot. Ich hätte Deinen Source genauer lesen sollen.

Du kannst doch nicht die Datei löschen, die Du gerade selbst benutzt. Selbstmord gibt es nur bei Lebewesen. :mrgreen:
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Do 06.09.07 16:06 
Die Datzei benutze ich aber nicht... daran habe ich gedacht und habe eine Datei vfSetupHelper.exe erstelt, darin sthet der Code
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Do 06.09.07 16:11 
Kleiner Tipp, lass dir mal mit GetLastError den Windows-Fehlercode ausgeben, dann weißt du auch, warum es nicht geht.
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Do 06.09.07 16:15 
Werde es Versuchen...
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Do 06.09.07 16:20 
Error-Code ist Null (?)
Aber ich bekomme im Taskmanager unter Prozesse auch immer noch die VokabelFit.exe zu sehen, trotzdem ich Application.Terminate angegeben hab...
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6395
Erhaltene Danke: 149

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Do 06.09.07 17:11 
user profile iconWolle92 hat folgendes geschrieben:
Die Datzei benutze ich aber nicht... daran habe ich gedacht und habe eine Datei vfSetupHelper.exe erstelt, darin sthet der Code
Stimmt. Du holst ja nur den Pfad Deiner Applikation. Dann kann das ja nur bedeuten, dass DeleteFile nicht geklappt hat. Das dürfte z.B. der Fall sein, wenn die Applikation noch läuft oder Dir die Rechte zum Löschen fehlen.
hathor
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Do 06.09.07 17:47 
Falls Dein Programm noch läuft, muss es erst beendet werden, bevor es gelöscht werden kann:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
//uses TlHelp32
function KillProcess(const ExeName: String): Boolean;
var
  Prozess        : TProcessEntry32;
  h              : THandle;
begin
  Result := False;
  Prozess.dwSize := SizeOf(Prozess);
  h := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  try
   If Process32First(h, Prozess) then
    Repeat
     If AnsiLowerCase(Prozess.szExeFile) = AnsiLowerCase(ExeName) then
     Result := TerminateProcess(OpenProcess(Process_Terminate, False, Prozess.th32ProcessID), 1);
    Until (Not Process32Next(h, Prozess)) or Result;
  finally
   CloseHandle(h);
  end;
end;

//vor dem Löschbefehl einfügen:
    KillProcess('VokabelFit.exe');
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Fr 07.09.07 08:02 
Danke, es klappt!