其實網上已經有很多的例子了,不過對于和我一樣的初學者來說難免會遇到問題,我試過win32Api函數,也試過用removeDirectory,但都刪不掉指定目錄,分析了一下,很顯然,是這些函數只是單純的刪除空文件夾,非空文件夾刪不了的。也不知是不是我用的不對,總之用了下面這個函數挺靈的,帖出來大家分享下。
1
function TMainForm.DeleteDirectory(NowPath: string): Boolean; //刪除整個目錄
2
var
3
search: TSearchRec;
4
ret: integer;
5
key: string;
6
begin
7
if NowPath[Length(NowPath)] <> '\' then
8
NowPath := NowPath + '\';
9
key := Nowpath + '*.*';
10
ret := findFirst(key, faanyfile, search);
11
while ret = 0 do begin
12
if ((search.Attr and fadirectory) = faDirectory)
13
then begin
14
if (Search.Name <> '.') and (Search.name <> '..') then
15
DeleteDirectory(NowPath + Search.name);
16
end else begin
17
if ((search.attr and fadirectory) <> fadirectory) then begin
18
deletefile(NowPath + search.name);
19
end;
20
end;
21
ret := FindNext(search);
22
end;
23
findClose(search);
24
removedir(NowPath);
25
result := True;
26
end;
27

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
