C# Send Files or Folder to Recycle Bin
-
Monday, December 04, 2006 6:20 PMHow do you do this? Does .NET 2.0 have a built in function for handling this?
All Replies
-
Monday, December 04, 2006 7:08 PM
No built in functions that I can find, but a quick google search turns up this post.
For 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();
SHFileOperation(ref shf);
shf.wFunc = FO_DELETE;
shf.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
shf.pFrom = "C:\\test.txt";
- Proposed As Answer by Gary Le Sueur Monday, October 12, 2009 10:21 AM
-
Monday, December 04, 2006 7:09 PMModerator
To do this in C# 2.0 do these steps:- Add a reference to Microsoft.VisualBasic assembly. The needed class is found in this library.
- Add this using statement to the top of the file using Microsoft.VisualBasic.FileIO;
- Use FileSystem.DeleteFile to delete a file, it has the option to specify recycle bin or not.
- Use FileSystem.DeleteDirectory to delete a directory with the option to specify to send it to the recycle bin or not.
-
Tuesday, December 05, 2006 4:48 AMThanks. I had actually just discovered that a little while ago. Thanks for your help guys.
-
Tuesday, December 05, 2006 10:41 AM
Hey soconne
Can u send the code that u have used to delete a file and put it in Recyclebin,
Byee and TC
-
Tuesday, December 05, 2006 2:52 PMModeratorPrashant: MSDN has an example of the deleting to the recycle bin How to: Delete a File in Visual Basic ... also remember one can F1 on any item in Visual Studio editor to be taken to help.<br>
-
Tuesday, December 05, 2006 4:42 PMHere's what I did, add Microsoft.VisualBasic as a reference and then do:
Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(filename or folder,
Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,
Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin); -
Thursday, December 07, 2006 11:42 AMIs in VB using Framework 2.0 with Visual Studio 2005 but should not be a problem to convert it to C#

Delete File:
My.Computer.FileSystem.DeleteFile("THE FILE TO DELETE", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin)
Delete Directory:
My.Computer.FileSystem.DeleteDirectory("THE DIRECTORY TO DELETE", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin)
Have fun and be carefull.
Tony -
Tuesday, November 10, 2009 4:59 PMNote that this line:
shf.pFrom = "C:\\test.txt";
Is wrong - pFrom must be double null terminated.
shf.pFrom = "C:\\text.txt\0"; is correct as per
http://msdn.microsoft.com/en-us/library/bb759795%28VS.85%29.aspx -
Friday, February 19, 2010 1:49 PM@Michael LetterleThanks it works fine.But the only problem I have is that the file in the trash as no name. Is there any solution to fix this issue?Thanks in advance
-
Thursday, January 20, 2011 12:39 PM
To do this in C# 2.0 do these steps:
- Add a reference to Microsoft.VisualBasic assembly. The needed class is found in this library.
I think there is a (good) reason why Microsoft has implemented this under VisualBasic namespace ... It is not a good practice to (ab)use it in such situations. If MS would like to use that it would have been implemented in another namespace (e.g. Microsoft.Windows.Shell or whatever ^^)
You should use a "shell operation" as suggested by "Michael Letterle"!
Programming is a kind of art but not all programmers are artists. -
Thursday, June 30, 2011 7:00 PMThis is because it was implemented to allow Visual Basic 6 code to work in Visual Basic.NET, hence why it is in the VisualBasic namespace. It in no way means you should not use it - after all, all it does is wrap a shell operation.
-
Friday, November 23, 2012 12:29 AMtested, it works! Tks a lot
Phuong

