Delphi刪除文件容易,但刪除正在使用的文件,那就需要手段了,因為正在使用的文件是不允許被刪除的,所以要想知道如何實現,或許你會從下面的代碼中得到啟發,其實很簡單,呵呵,不說了,看代碼:
001
unit Unit1;
002
interface
003
uses
004
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
005
StdCtrls, ExtCtrls;
006
const
007
FILE_DELETE=1;
008
FILE_RENAME=2;
009
type
010
TForm1 =
class
(TForm)
011
Button1: TButton;
012
Label1: TLabel;
013
Label2: TLabel;
014
RadioGroup1: TRadioGroup;
015
Edit1: TEdit;
016
Edit2: TEdit;
017
Button2: TButton;
018
Button3: TButton;
019
OpenDialog1: TOpenDialog;
020
procedure Button2Click(Sender: TObject);
021
procedure Button3Click(Sender: TObject);
022
procedure Button1Click(Sender: TObject);
023
procedure Edit2Change(Sender: TObject);
024
procedure RadioGroup1Click(Sender: TObject);
025
private
026
{ Private declarations }
027
public
028
{ Public declarations }
029
end;
030
var
031
Form1: TForm1;
032
implementation
033
{$R *.DFM}
034
function DeleteRenameFileAfterBoot(lpFileNameToSrc,lpFileNameToDes: PChar;flag:Uint): Boolean;
035
var
036
WindowsDirs: array [0..MAX_PATH + 1] of Char;
037
lpDirSrc,lpDirDes: array [0..MAX_PATH + 1] of Char;
038
VerPlatForm: TOSVersionInfoA;
039
StrLstDelte: TStrings;
040
filename,s :String;
041
i:integer;
042
begin
043
Result := FALSE;
044
ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
045
VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
046
GetVersionEx(VerPlatForm);
047
if
VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32s then
048
begin
049
SetLastError(ERROR_NOT_SUPPORTED);
050
Exit;
051
end
052
else
if
VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_NT then
053
begin
054
if
flag=FILE_DELETE then
055
Result := MoveFileEx(PChar(lpFileNameToSrc), nil,
056
MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT)
057
else
if
(flag=FILE_RENAME) then
058
Result := MoveFileEx(lpFileNameToSrc, lpFileNameToDes,
059
MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT);
060
end
061
else
begin
062
StrLstDelte := TStringList.Create;
063
GetWindowsDirectory(WindowsDirs, MAX_PATH + 1);
064
filename:=WindowsDirs;
065
if
filename[length(filename)]<>
'\' then filename:=filename+'
\';
066
filename:=filename+
'wininit.ini'
;
067
if
FileExists(filename) then
068
StrLstDelte.LoadFromFile(filename);
069
if
StrLstDelte.IndexOf(
'[rename]'
) = -1 then
070
StrLstDelte.Add(
'[rename]'
);
071
GetShortPathName(lpFileNameToSrc, lpDirSrc, MAX_PATH + 1);
072
if
fileexists(lpFileNameToDes) then
073
GetShortPathName(lpFileNameToDes, lpDirDes, MAX_PATH + 1)
074
else
begin
075
s:=extractfilename(lpFileNameToDes);
076
i:=pos(
'.'
,s);
077
if
(i=0) then
078
begin
079
if
length(s)>8 then raise exception.create(
'不是有效的短文件名(8+3格式)!'
);
080
end
081
else
begin
082
if
(i-1>8)or(length(s)-i>3) then raise exception.create(
'不是有效的短文件名(8+3格式)!'
);
083
end;
084
strcopy(lpDirDes,lpFileNameToDes);
085
end;
086
if
(flag=FILE_DELETE) then {刪除}
087
StrLstDelte.Insert(StrLstDelte.IndexOf(
'[rename]'
) + 1,
'NUL='
+
string
(lpDirSrc))
088
else
if
(flag=FILE_RENAME) then {改名}
089
StrLstDelte.Insert(StrLstDelte.IndexOf(
'[rename]'
) + 1,
string
(lpDirDes)+
'='
+
string
(lpDirSrc));
090
091
StrLstDelte.SaveToFile(filename);
092
Result := TRUE;
093
StrLstDelte.Free;
094
end;
095
end;
096
procedure TForm1.Button2Click(Sender: TObject);
097
begin
098
if
OpenDialog1.Execute then
099
edit1.text:=OpenDialog1.FileName;
100
end;
101
102
procedure TForm1.Button3Click(Sender: TObject);
103
begin
104
if
OpenDialog1.Execute then
105
edit2.text:=OpenDialog1.FileName;
106
end;
107
procedure TForm1.Button1Click(Sender: TObject);
108
var
109
i:
uint
;
110
begin
111
if
RadioGroup1.ItemIndex=0 then i:=FILE_DELETE
112
else
i:=FILE_RENAME;
113
if
edit1.text=
''
then raise exception.create(
'源文件為空!'
);
114
if
(i=FILE_RENAME)and(edit2.text=
''
) then raise exception.create(
'目標文件為空!'
);
115
if
not DeleteRenameFileAfterBoot(pchar(edit1.text),pchar(edit2.text),i) then
116
showmessage(
'出錯了'
)
117
else
showmessage(
'操作完成'
);
118
end;
119
120
procedure TForm1.Edit2Change(Sender: TObject);
121
var
122
VerPlatForm: TOSVersionInfoA;
123
buf: array [0..MAX_PATH + 1] of Char;
124
begin
125
if
not fileexists(edit2.text) then exit;
126
ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
127
VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
128
GetVersionEx(VerPlatForm);
129
if
VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_Windows then
130
begin
131
GetShortPathName(pchar(edit2.text), buf, MAX_PATH + 1);
132
edit2.text:=buf;
133
end;
134
end;
135
procedure TForm1.RadioGroup1Click(Sender: TObject);
136
begin
137
edit2.Enabled:=RadioGroup1.ItemIndex=1;
138
button2.Enabled:=RadioGroup1.ItemIndex=1;
139
end;
140
end.
其實就是利用Windows重啟的瞬間來刪除或移動文件。