如何在程序中将文件放入回收站(100分)

  • 主题发起人 主题发起人 Roo
  • 开始时间 开始时间
前天刚从&lt;a href="http://www.nease.net/~liyaping"&gt;边缘软件工作室&lt;/a&gt;拉回来的例子,借花献佛!<br>&nbsp; &nbsp; &nbsp;可以使用 SHFileOperation(), 值得注意的是档名 list 中是以 #0 分隔, 最後一个项目必须以双 #0 字元结束,<br>&nbsp; &nbsp; &nbsp;以下分别是删除""个目录(含字目录) 与档案的用法示例: <br>&nbsp; &nbsp; &nbsp;// Test for delete a folder and move files into Recycle Bin<br>&nbsp; &nbsp; &nbsp;// Change the APath variable as you need. &nbsp; &nbsp; // &nbsp;<br>&nbsp; &nbsp;uses ..., ShellAPI;<br>&nbsp; &nbsp;procedure TForm1.Button1Click(Sender: TObject); &nbsp; &nbsp; <br>&nbsp; &nbsp;var<br>&nbsp; &nbsp; &nbsp; APath &nbsp; : AnsiString; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; lpFileOp: TSHFileOpStruct; &nbsp; &nbsp; <br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;APath := 'D:/temp/test123'#0#0; // must end with double-#0<br>&nbsp; &nbsp; &nbsp; &nbsp;with lpFileOp do &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp;begin &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Wnd := Self.Handle;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wFunc := FO_DELETE; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pFrom := pchar(APath);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pTo := nil;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fFlags := FOF_ALLOWUNDO;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hNameMappings := nil;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lpszProgressTitle := nil;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fAnyOperationsAborted := True;<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp;if SHFileOperation(lpFileOp) = 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowMessage('SHFileOperation OK.')<br>&nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowMessage('SHFileOperation Fail!!'); &nbsp; &nbsp; <br>end; <br>&nbsp; 档案s 删除并送至资源回收筒?<br>&nbsp; procedure TForm1.Button1Click(Sender: TObject);<br>&nbsp; var<br>&nbsp; &nbsp; &nbsp; &nbsp;APath &nbsp; : AnsiString; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp;lpFileOp: TSHFileOpStruct;<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;// must end with double-#0 &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; APath :='D:/temp/test123/l.txt'#0'D:/temp/test123/l3.txt'#0#0;<br>&nbsp; &nbsp; with lpFileOp do &nbsp;<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Wnd := Self.Handle;<br>&nbsp; &nbsp; &nbsp; wFunc := FO_DELETE; <br>&nbsp; &nbsp; &nbsp; pFrom := pchar(APath);<br>&nbsp; &nbsp; &nbsp; pTo := nil;<br>&nbsp; &nbsp; &nbsp; fFlags := FOF_ALLOWUNDO; &nbsp;<br>&nbsp; &nbsp; &nbsp; hNameMappings := nil;<br>&nbsp; &nbsp; &nbsp; lpszProgressTitle := nil; <br>&nbsp; &nbsp; &nbsp; fAnyOperationsAborted := True;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; if SHFileOperation(lpFileOp) = 0 then<br>&nbsp; &nbsp; &nbsp; ShowMessage('SHFileOperation OK.')<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; ShowMessage('SHFileOperation Fail!!'); &nbsp; &nbsp; <br>end;<br>Good Luck!<br><br>
 
也是借花献佛:-)<br>How can I delete a file to the<br>Recycle Bin?<br><br>From: "Ed Lagerburg" &lt;lagerbrg@euronet.nl&gt; <br><br><br>program del;<br><br>uses<br>&nbsp;ShellApi;<br><br>//function SHFileOperation(const lpFileOp: TSHFileOpStruct): Integer; stdcall;<br><br>Var T:TSHFileOpStruct;<br>&nbsp; &nbsp; P:String;<br>begin<br>&nbsp; P:='C:/Windows/System/EL_CONTROL.CPL';<br>&nbsp; With T do<br>&nbsp; Begin<br>&nbsp; &nbsp; Wnd:=0;<br>&nbsp; &nbsp; wFunc:=FO_DELETE;<br>&nbsp; &nbsp; pFrom:=Pchar(P);<br>&nbsp; &nbsp; fFlags:=FOF_ALLOWUNDO<br>&nbsp; End;<br>&nbsp; SHFileOperation(T);<br>End.<br><br><br>From: bstowers@pobox.com (Brad Stowers)<br><br>There are some other quirks you should be aware of, too: <br><br>&nbsp; &nbsp; &nbsp; Give the complete file path for every file specified. Do<br>&nbsp; &nbsp; &nbsp; not rely on the current directory, even if you change to it<br>&nbsp; &nbsp; &nbsp; right before the call. The SHFileOperation API is not<br>&nbsp; &nbsp; &nbsp; "smart" enough to use the current directory if one is not<br>&nbsp; &nbsp; &nbsp; given for undo information. So, even if you give the<br>&nbsp; &nbsp; &nbsp; FOF_ALLOWUNDO flag, it will not move deleted files to the<br>&nbsp; &nbsp; &nbsp; recycle bin because it doesn't know what path they came<br>&nbsp; &nbsp; &nbsp; from, and thus couldn't restore them to their original<br>&nbsp; &nbsp; &nbsp; location from the recycle bin. It will simply delete the<br>&nbsp; &nbsp; &nbsp; file from the current directory. <br><br>&nbsp; &nbsp; &nbsp; MS has a documentation correction about the pFrom member.<br>&nbsp; &nbsp; &nbsp; It says that for multiple files, each filename is seperated<br>&nbsp; &nbsp; &nbsp; by a NULL (#0) character, and the whole thing is terminated<br>&nbsp; &nbsp; &nbsp; by double NULLs. You need the double NULL whether or not<br>&nbsp; &nbsp; &nbsp; you are passing multiple filenames. Sometimes it will work<br>&nbsp; &nbsp; &nbsp; correctly without them, but often not. That's because<br>&nbsp; &nbsp; &nbsp; sometimes you get lucky and the memory following the end of<br>&nbsp; &nbsp; &nbsp; your string has a NULL there. <br><br>An example of how to do this would be:<br><br>var<br>&nbsp; FileList: string;<br>&nbsp; FOS: TShFileOpStruct;<br>begin<br>&nbsp; FileList := 'c:/delete.me'#0'c:/windows/temp.$$$'#0#0;<br>&nbsp; { if you were using filenames in string variables: }<br>&nbsp; FileList := Filename1 + #0 + Filename2 + #0#0;<br><br>&nbsp; FOS.pFrom := PChar(FileList);<br><br>&nbsp; // blah blah blah<br>end;
 
也是借花献佛:-) from uddf<br>How can I delete a file to the<br>Recycle Bin?<br><br>From: "Ed Lagerburg" &lt;lagerbrg@euronet.nl&gt; <br><br><br>program del;<br><br>uses<br>&nbsp;ShellApi;<br><br>//function SHFileOperation(const lpFileOp: TSHFileOpStruct): Integer; stdcall;<br><br>Var T:TSHFileOpStruct;<br>&nbsp; &nbsp; P:String;<br>begin<br>&nbsp; P:='C:/Windows/System/EL_CONTROL.CPL';<br>&nbsp; With T do<br>&nbsp; Begin<br>&nbsp; &nbsp; Wnd:=0;<br>&nbsp; &nbsp; wFunc:=FO_DELETE;<br>&nbsp; &nbsp; pFrom:=Pchar(P);<br>&nbsp; &nbsp; fFlags:=FOF_ALLOWUNDO<br>&nbsp; End;<br>&nbsp; SHFileOperation(T);<br>End.<br><br><br>From: bstowers@pobox.com (Brad Stowers)<br><br>There are some other quirks you should be aware of, too: <br><br>&nbsp; &nbsp; &nbsp; Give the complete file path for every file specified. Do<br>&nbsp; &nbsp; &nbsp; not rely on the current directory, even if you change to it<br>&nbsp; &nbsp; &nbsp; right before the call. The SHFileOperation API is not<br>&nbsp; &nbsp; &nbsp; "smart" enough to use the current directory if one is not<br>&nbsp; &nbsp; &nbsp; given for undo information. So, even if you give the<br>&nbsp; &nbsp; &nbsp; FOF_ALLOWUNDO flag, it will not move deleted files to the<br>&nbsp; &nbsp; &nbsp; recycle bin because it doesn't know what path they came<br>&nbsp; &nbsp; &nbsp; from, and thus couldn't restore them to their original<br>&nbsp; &nbsp; &nbsp; location from the recycle bin. It will simply delete the<br>&nbsp; &nbsp; &nbsp; file from the current directory. <br><br>&nbsp; &nbsp; &nbsp; MS has a documentation correction about the pFrom member.<br>&nbsp; &nbsp; &nbsp; It says that for multiple files, each filename is seperated<br>&nbsp; &nbsp; &nbsp; by a NULL (#0) character, and the whole thing is terminated<br>&nbsp; &nbsp; &nbsp; by double NULLs. You need the double NULL whether or not<br>&nbsp; &nbsp; &nbsp; you are passing multiple filenames. Sometimes it will work<br>&nbsp; &nbsp; &nbsp; correctly without them, but often not. That's because<br>&nbsp; &nbsp; &nbsp; sometimes you get lucky and the memory following the end of<br>&nbsp; &nbsp; &nbsp; your string has a NULL there. <br><br>An example of how to do this would be:<br><br>var<br>&nbsp; FileList: string;<br>&nbsp; FOS: TShFileOpStruct;<br>begin<br>&nbsp; FileList := 'c:/delete.me'#0'c:/windows/temp.$$$'#0#0;<br>&nbsp; { if you were using filenames in string variables: }<br>&nbsp; FileList := Filename1 + #0 + Filename2 + #0#0;<br><br>&nbsp; FOS.pFrom := PChar(FileList);<br><br>&nbsp; // blah blah blah<br>end;
 
多人接受答案了。
 
后退
顶部