我使用带有SHFileOperationW参数的FO_DELETE函数将文件移动到回收站(如果没有禁用回收站)。
问题是当我以非管理用户的身份登录,并以admin身份运行我的应用程序时。这些文件被移动到管理员的回收站。
是否有可能将文件移动到当前日志记录的非管理用户的回收站?
我的想法是运行单独的非提升过程,并将其从那里移到回收站。但我不知道是否有更好的解决办法。我试图在网上找到答案,但没有成功。
发布于 2017-11-22 09:42:21
我建议在Delphi XE 10上进行以下例程测试.
function File2Trash(const FileName: string): boolean;
var
fos: TSHFileOpStruct;
begin
FillChar(fos, SizeOf(fos), 0);
with fos do
begin
wFunc := FO_DELETE;
pFrom := PChar(ExpandFileName(FileName)+#0#0);
fFlags := FOF_ALLOWUNDO or FOF_NOCONFIRMATION or FOF_SILENT or FOF_NOERRORUI;
end;
Result := (0 = ShFileOperation(fos));
end;有些细节很重要:
发布于 2013-11-17 17:19:12
当我需要使用这个例程时,我使用了以下命令。我希望这是有用的。
使用Delphi 2010和Windows 8进行测试。
procedure TForm2.Button1Click(Sender: TObject);
var
vMsg : string;
begin
// If want permanently delete
//deletefile(edit1.text);
SendFileToTrash(edit1.Text, vMsg);
if (vMsg = '') then
begin
ShowMessage('File sent to the trash.');
end else begin
ShowMessage(vMsg);
end;
end;
procedure TForm2.SendFileToTrash(const aFileName: TFileName; var MsgError: string);
var
Op: TSHFileOpStruct;
begin
{Very importante
Include in Uses SysUtils and ShellAPI;
}
MsgError := '';
if not (FileExists(aFileName)) then
begin
MsgError := 'File not found.';
Exit;
end;
FillChar(Op, SizeOf(Op), 0);
Op.wFunc := FO_DELETE;
Op.pFrom := PChar(aFileName+#0);
Op.fFlags := FOF_ALLOWUNDO or FOF_NOCONFIRMATION or FOF_SILENT;
if (ShFileOperation(Op) <> 0) then
begin
MsgError := 'Could not send the file to the trash.';
end;
end;https://stackoverflow.com/questions/19932794
复制相似问题