假设要安全地删除c:\temp.tmp文件,可用以下代码实现。 Var T:TSHFileOpStruct; filename:String; begin filename:='c:\temp.tmp'; with T do begin Wnd:=0; wFunc:=FO_DELETE; pFrom:=Pchar(filename); fFlags:=FOF_ALLOWUNDO end; try SHFileOperation(T); except on eaccessviolation do showmessage('删除命令已被取消!'); end; 注意,如果指定文件不存在,或者在确认删除时按了“取消”,都会执行except后的语句。 在uses语句中加上shellapi。
例子如下:// Delphiprogram del;uses ShellApi; { 利用ShellApi中: function SHFileOperation(const lpFileOp: TSHFileOpStruct): Integer; stdcall; }Var T:TSHFileOpStruct; P:String; begin P:='C:\Windows\System\EL_CONTROL.CPL'; With T do Begin Wnd:=0; wFunc:=FO_DELETE; pFrom:=Pchar(P); fFlags:=FOF_ALLOWUNDO End; SHFileOperation(T); End.注意: 1. 给出文件的绝对路径名,否则可能不能恢复; 2. MS的文档说对于多个文件,每个文件名必须被#)字符分隔,而整个字符串必须用两个#0结束。
When used to delete a file, SHFileOperation will attempt to place the deleted file in the Recycle Bin. If you wish to delete a file and guarantee that it will not be placed in the Recycle Bin, use DeleteFile. 直接使用DeleteFile函数将文件删除,不保存至回收站!
清除回收站内容: uses ShellApi; { 利用ShellApi中: function SHFileOperation(const lpFileOp: TSHFileOpStruct): Integer; stdcall; }Var T:TSHFileOpStruct; P:String; begin P:='C:\Windows\System\EL_CONTROL.CPL'; With T do Begin Wnd:=0; wFunc:=FO_DELETE; pFrom:=Pchar(P); fFlags:=FOF_ALLOWUNDO End; SHFileOperation(T); End.
Var T:TSHFileOpStruct;
filename:String;
begin
filename:='c:\temp.tmp';
with T do
begin
Wnd:=0;
wFunc:=FO_DELETE;
pFrom:=Pchar(filename);
fFlags:=FOF_ALLOWUNDO
end;
try
SHFileOperation(T);
except
on eaccessviolation do showmessage('删除命令已被取消!');
end;
注意,如果指定文件不存在,或者在确认删除时按了“取消”,都会执行except后的语句。
在uses语句中加上shellapi。
{ 利用ShellApi中: function SHFileOperation(const lpFileOp: TSHFileOpStruct): Integer; stdcall; }Var T:TSHFileOpStruct;
P:String;
begin
P:='C:\Windows\System\EL_CONTROL.CPL';
With T do
Begin
Wnd:=0;
wFunc:=FO_DELETE;
pFrom:=Pchar(P);
fFlags:=FOF_ALLOWUNDO
End;
SHFileOperation(T);
End.注意:
1. 给出文件的绝对路径名,否则可能不能恢复;
2. MS的文档说对于多个文件,每个文件名必须被#)字符分隔,而整个字符串必须用两个#0结束。
这个函数和其他一些函数没有被delphi声明
必须自己从shell32.dll中声明
直接使用DeleteFile函数将文件删除,不保存至回收站!
uses ShellApi;
{ 利用ShellApi中: function SHFileOperation(const lpFileOp: TSHFileOpStruct): Integer; stdcall; }Var T:TSHFileOpStruct;
P:String;
begin
P:='C:\Windows\System\EL_CONTROL.CPL';
With T do
Begin
Wnd:=0;
wFunc:=FO_DELETE;
pFrom:=Pchar(P);
fFlags:=FOF_ALLOWUNDO
End;
SHFileOperation(T);
End.