Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you place a file in recycle bin instead of delete?

Tags:

c++

c#

.net

io

windows

Programmatic solution of course...

like image 806
Brian Leahy Avatar asked Aug 20 '08 08:08

Brian Leahy


People also ask

Does putting a file in the recycling bin delete it?

But apparently, permanently deleting all your files and other important data even in the Recycle Bin is not enough. It is not technically "deleted." Although the operating system can't find it anymore, a copy of it is still accessible through your hard drive.

When you delete files from and they do not go to the Recycle Bin?

Right-click the Recycle Bin icon, and click Property. Opening the Recycle Bin Property dialog box, you will find in the Settings for selected location section that it's all because of the selection of Don't move files to the Recycle Bin. Remove files immediately when deleted, which results in direct deletion for files.


2 Answers

http://www.daveamenta.com/2008-05/c-delete-a-file-to-the-recycle-bin/

From above:

using Microsoft.VisualBasic;  string path = @"c:\myfile.txt"; FileIO.FileSystem.DeleteDirectory(path,      FileIO.UIOption.OnlyErrorDialogs,      RecycleOption.SendToRecycleBin); 
like image 179
TK. Avatar answered Sep 20 '22 03:09

TK.


You need to delve into unmanaged code. Here's a static class that I've been using:

public static class Recycle {     private const int FO_DELETE = 3;     private const int FOF_ALLOWUNDO = 0x40;     private const int FOF_NOCONFIRMATION = 0x0010;      [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);      public static void DeleteFileOperation(string filePath)     {         SHFILEOPSTRUCT fileop = new SHFILEOPSTRUCT();         fileop.wFunc = FO_DELETE;         fileop.pFrom = filePath + '\0' + '\0';         fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;          SHFileOperation(ref fileop);     } } 

Addendum:

  • Tsk tsk @ Jeff for "using Microsoft.VisualBasic" in C# code.
  • Tsk tsk @ MS for putting all the goodies in VisualBasic namespace.
like image 31
Ishmaeel Avatar answered Sep 24 '22 03:09

Ishmaeel