Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Moving files to the Recycle Bin

363 views
Skip to first unread message

JimConnell

unread,
Jun 12, 2002, 8:31:11 PM6/12/02
to
I've been trying to figure out how to programmatically delete files so that
they end up in the Recycle Bin. I've been through the obvious beginner things
that don't work and now realize that I need to use SHFileOperation to do it,
and understand why.

I haven't found anything explaining how to do this in C#, and before I spend a
whole bunch of time figuring this out, I thought I'd ask if anyone else has
already done this, or something similar, and would be willing to share their
code.

Maybe we could end up with an (externally) simple function to do this that
could be put in some library somewhere.

I'm really surprised that everyone isn't doing this already; the Recycle Bin
seems like a perfect place to stash old copies of a file, for instance a
document, rather than just overwriting it with a revision as is the custom.

-jimc

Leo Chen [MS]

unread,
Jun 17, 2002, 5:50:29 AM6/17/02
to
Hi Jimc,

You can find some previous posts by MVP Mattias and Nicholas. Here is the
code for your reference:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
public struct SHFILEOPSTRUCT
{
public IntPtr hwnd;
[MarshalAs(UnmanagedType.U4)] public int wFunc;
public string pFrom;
public string pTo;
public short fFlags;
[MarshalAs(UnmanagedType.Bool)] public bool fAnyOperationsAborted;
public IntPtr hNameMappings;
public string lpszProgressTitle;
}


[DllImport("shell32.dll", CharSet=CharSet.Auto)]
static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
const int FO_DELETE = 3;
const int FOF_ALLOWUNDO = 0x40;
const int FOF_NOCONFIRMATION = 0x10; //Don't prompt the user.;

To use the API:

SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
shf.wFunc = FO_DELETE;
shf.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
shf.pFrom = "C:\\test.txt";

SHFileOperation(ref shf);

I hope this helps.

Best Regards,
Leo Chen

This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
| Lines: 18
| X-Admin: ne...@aol.com
| From: jimco...@aol.com (JimConnell)
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Date: 13 Jun 2002 00:31:11 GMT
| Organization: AOL http://www.aol.com
| Subject: Moving files to the Recycle Bin
| Message-ID: <20020612203111...@mb-cu.aol.com>
| Path:
cpmsftngxa08!cppssbbsa01.microsoft.com!news-out.cwix.com!newsfeed.cwix.com!n
ntp.abs.net!uunet!dca.uu.net!ash.uu.net!ngpeer.news.aol.com!audrey05.news.ao
l.com!not-for-mail
| Xref: cpmsftngxa08 microsoft.public.dotnet.languages.csharp:67580
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

0 new messages