1

I am using SHFileOperationW function with FO_DELETE parameter to move a file to recycle bin (if recycle bin is not disabled).

The problem is when I log as non-admin user and run my application as admin. The files are moved to admin's recycle bin.

Is it possible to make it so the files are moved to the currenly logged non-admin user's recycle bin?

My idea was to run separate non-elevated process and move it to recycle bin from there. But I am not sure if a better solution exists. I tried to find answer on internet, but did not succeed.

2
  • 3
    Seems the proper thing would not to launch your application as administrator in the first place. If you have a task that requires administrative privilege, spawn a separate process that requests elevation. Nov 24, 2013 at 18:30
  • thank you, I have solved it as you say.
    – user2614370
    Dec 4, 2013 at 12:44

2 Answers 2

0

I would propose the following routine - tested on 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;

Some details are important:

  • no relative path should be used, existing path should be expanded to full one;
  • adding #0#0 to the end of file name is the must;
  • to keep this operation silent I would propose to use one more flag FOF_NOERRORUI.
-1

When I needed to use this routine, I used the following commands. I hope it is useful.

Tests with Delphi 2010 and 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;
3
  • 1
    Thanks, but unfortunatelly this does not answer my question. I use almost same code.
    – user2614370
    Nov 20, 2013 at 12:28
  • see detail of this line [Op.pFrom := PChar(aFileName+#0);], but specifically the +#0. Without this code snippet does not work. Nov 20, 2013 at 14:18
  • I think that you did not understand the question. Please read my question again. Thanks.
    – user2614370
    Nov 21, 2013 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.