the key is Win32 API: SHFileOperationHow can I delete a file to the Recycle Bin?From: "Ed Lagerburg" <[email protected]>
program del;uses
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.
From: [email protected] (Brad Stowers)There are some other quirks you should be aware of, too: Give the complete file path for every file specified. Do not rely on the current directory, even if you change
to it right before the call. The SHFileOperation API is not "smart" enough to use the current directory if
one is not given for undo information. So, even if you give the FOF_ALLOWUNDO flag, it will not move
deleted files to the recycle bin because it doesn't know what path they came from, and thus couldn't
restore them to their original location from the recycle bin. It will simply delete the file from the current
directory. MS has a documentation correction about the pFrom member. It says that for multiple files, each filename
is seperated by a NULL (#0) character, and the whole thing is terminated by double NULLs. You need
the double NULL whether or not you are passing multiple filenames. Sometimes it will work correctly
without them, but often not. That's because sometimes you get lucky and the memory following the end of
your string has a NULL there. An example of how to do this would be:var
FileList: string;
FOS: TShFileOpStruct;
begin
FileList := 'c:\delete.me'#0'c:\windows\temp.$$$'#0#0;
{ if you were using filenames in string variables: }
FileList := Filename1 + #0 + Filename2 + #0#0; FOS.pFrom := PChar(FileList); // blah blah blah
end;
program del;uses
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.
From: [email protected] (Brad Stowers)There are some other quirks you should be aware of, too: Give the complete file path for every file specified. Do not rely on the current directory, even if you change
to it right before the call. The SHFileOperation API is not "smart" enough to use the current directory if
one is not given for undo information. So, even if you give the FOF_ALLOWUNDO flag, it will not move
deleted files to the recycle bin because it doesn't know what path they came from, and thus couldn't
restore them to their original location from the recycle bin. It will simply delete the file from the current
directory. MS has a documentation correction about the pFrom member. It says that for multiple files, each filename
is seperated by a NULL (#0) character, and the whole thing is terminated by double NULLs. You need
the double NULL whether or not you are passing multiple filenames. Sometimes it will work correctly
without them, but often not. That's because sometimes you get lucky and the memory following the end of
your string has a NULL there. An example of how to do this would be:var
FileList: string;
FOS: TShFileOpStruct;
begin
FileList := 'c:\delete.me'#0'c:\windows\temp.$$$'#0#0;
{ if you were using filenames in string variables: }
FileList := Filename1 + #0 + Filename2 + #0#0; FOS.pFrom := PChar(FileList); // blah blah blah
end;
...
SHFILEOPSTRUCT s;
s.hwnd=NULL;
s.wFunc=FO_DELETE;
s.pFrom="e:\\t.txt";
s.fFlags=FOF_ALLOWUNDO and FOF_NOCONFIRMATION ;
SHFileOperation(&s);
...
其中的FOF_ALLOWUNDO表示放到回收站中,FOF_NOCONFIRMATION表示是否提示,如不加此项,会提问是否放到回收站中。
#include <shellapi.h>
...
SHFILEOPSTRUCT s;
s.hwnd=NULL;
s.wFunc=FO_DELETE;
s.pFrom="e:\\t.txt";
s.fFlags=FOF_ALLOWUNDO and FOF_NOCONFIRMATION ;
SHFileOperation(&s);
...
其中的FOF_ALLOWUNDO表示放到回收站中,FOF_NOCONFIRMATION表示是否提示,如不加此项,会提问是否放到回收站中。为什么' and '会变成了'and'?!