Delete files to recycle bin

  • Thread starter Thread starter rob
  • Start date Start date
Hi Rob,
Does C# 2005 have an easy way to delete files to the recycle bin?

The .NET Framework doesn't have built-in support for this, but you can call
the Shell Win32 API function SHFileOperation along with the flag
FOF_ALLOWUNDO.

Here's a C# example, though I haven't tested this:
http://www.personalmicrocosms.com/Pages/dotnettips.aspx?c=19&t=22

Another example from PInvoke.net, which is useful if you want to call Win32
APIs from managed code:
http://www.pinvoke.net/default.aspx/shell32/SHFileOperation.html

And finally some text from the documentation of SHFileOperation from MSDN:
http://msdn.microsoft.com/library/d...shell/reference/functions/shfileoperation.asp

"When used to delete a file, SHFileOperation permanently deletes the file
unless you set the FOF_ALLOWUNDO flag in the fFlags member of the
SHFILEOPSTRUCT structure pointed to by lpFileOp. Setting that flag sends the
file to the Recycle Bin. If you want to delete a file and guarantee that it
is not placed in the Recycle Bin, use DeleteFile."

Hope this solves your problem.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
Hi Rob,


The .NET Framework doesn't have built-in support for this, but you can call
the Shell Win32 API function SHFileOperation along with the flag
FOF_ALLOWUNDO.

Here's a C# example, though I haven't tested this:
http://www.personalmicrocosms.com/Pages/dotnettips.aspx?c=19&t=22

Terrific, Jani. I think that fills in the blanks. I suspected that I'd
have to go with Pinvoke. My first run at SHFileOp didn't work, but I
think you've clarified the missing flag.

I'm surprised that the .NET 2005 library still has holes in some
pretty fundamental operations. This seems like it would have been easy
for MS to wrap in the C# library.

Thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Windows 7 Recycle Bins Corrupt - No File List 1
restore files still in recycle bin 3
Recycle bin problems 5
Delete File / Recycle Bin 8
Recycle bin 8
Deleting and the recycle bin 3
Windows 7 Problem opening a folder or file 1
Discover when Recycle Bin emptied? 3

Back
Top