-
Notifications
You must be signed in to change notification settings - Fork 64
/
DN.FileService.pas
129 lines (116 loc) · 2.87 KB
/
DN.FileService.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
unit DN.FileService;
interface
uses
DN.FileService.Intf;
type
TDNFileService = class(TInterfacedObject, IDNFileService)
private
FRootKey: string;
public
constructor Create(const ARootKey: string);
procedure RegisterDirectoryForDeletion(const ADirectory: string);
procedure RegisterFileForDeletion(const AFileName: string);
procedure Cleanup;
end;
implementation
uses
Classes,
IOUtils,
Registry;
const
CRootDir = 'Delphinus.FileCleanupService';
CDirectories = 'Directories';
CFiles = 'Files';
{ TDNFileService }
constructor TDNFileService.Create(const ARootKey: string);
begin
inherited Create();
FRootKey := ARootKey;
end;
procedure TDNFileService.Cleanup;
var
LRegistry: TRegistry;
LEntries: TStringList;
LEntry: string;
begin
LRegistry := TRegistry.Create();
LEntries := TStringList.Create();
try
if LRegistry.OpenKey(FRootKey, False) then
begin
if LRegistry.OpenKey(CRootDir, False) then
begin
if LRegistry.OpenKey(CFiles, False) then
begin
try
LRegistry.GetValueNames(LEntries);
for LEntry in LEntries do
if TFile.Exists(LEntry) then
TFile.Delete(LEntry);
finally
LRegistry.CloseKey();
end;
LRegistry.DeleteKey(TPath.Combine(TPath.Combine(FRootKey, CRootDir), CFiles));
end;
if LRegistry.OpenKey(CDirectories, False) then
begin
try
LRegistry.GetValueNames(LEntries);
for LEntry in LEntries do
if TDirectory.Exists(LEntry) then
TDirectory.Delete(LEntry, True);
finally
LRegistry.CloseKey();
end;
LRegistry.DeleteKey(TPath.Combine(TPath.Combine(FRootKey, CRootDir), CDirectories));
end;
LRegistry.CloseKey();
LRegistry.DeleteKey(TPath.Combine(FRootKey, CRootDir));
end;
end;
finally
LEntries.Free;
LRegistry.Free;
end;
end;
procedure TDNFileService.RegisterDirectoryForDeletion(const ADirectory: string);
var
LRegistry: TRegistry;
begin
LRegistry := TRegistry.Create();
try
if LRegistry.OpenKey(FRootKey, False) then
begin
if LRegistry.OpenKey(CRootDir, True) then
begin
if LRegistry.OpenKey(CDirectories, True) then
begin
LRegistry.WriteString(ADirectory, '');
end;
end;
end;
finally
LRegistry.Free;
end;
end;
procedure TDNFileService.RegisterFileForDeletion(const AFileName: string);
var
LRegistry: TRegistry;
begin
LRegistry := TRegistry.Create();
try
if LRegistry.OpenKey(FRootKey, False) then
begin
if LRegistry.OpenKey(CRootDir, True) then
begin
if LRegistry.OpenKey(CFiles, True) then
begin
LRegistry.WriteString(AFileName, '');
end;
end;
end;
finally
LRegistry.Free;
end;
end;
end.