?
Solved

Sending a file to the recycling bin

Posted on 2005-04-11
4
Medium Priority
?
211 Views
Last Modified: 2010-04-16
I delete files by calling File.Delete( filePath );

but I'd like to also add it to the recycling bin.  Does anybody know how to do this?


Thanks,
Bob
0
Comment
Question by:ba272
  • 2
4 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 600 total points
ID: 13759588
SHFileOperation function places the deleted file in the Recycle Bin. This article shows how to use this function:

http://www.codeproject.com/csharp/csdoesshell2.asp

See Section 2: Doing File Operations. You need to use FOF_SILENT and FOF_NOERRORUI flags to run this function without user interface.
0
 
LVL 5

Accepted Solution

by:
beryl666 earned 1400 total points
ID: 13759697
Conversion Code is c#:
// TODO: NotImplemented statement: ICSharpCode.SharpRefactory.Parser.AST.VB.OptionStrictDeclaration
// TODO: NotImplemented statement: ICSharpCode.SharpRefactory.Parser.AST.VB.OptionExplicitDeclaration
using System.IO;
struct Module1
{
 private struct SHFILEOPSTRUCT
 {
   int hwnd;
   int wFunc;
   string pFrom;
   string pTo;
   short fFlags;
   bool fAnyOperationsAborted;
   int hNameMappings;
   string lpszProgressTitle;
 }
 private const short FO_DELETE = 3;
 private const short FOF_ALLOWUNDO = 64;
 private const short FOF_NOCONFIRMATION = 16;
 [System.Runtime.InteropServices.DllImport("shell32.dll", EntryPoint="SHFileOperationA")]
 private static extern int SHFileOperation(ref SHFILEOPSTRUCT lpFileOp);

 public int Recycle(ref string sPath)
 {
   SHFILEOPSTRUCT FileOp;
   if (!(File.Exists(sPath))) {
     MsgBox("Could not find " + sPath + "." + "\r\n" + "Please verify the path.");
     Recycle = -1;
return null;
   }
   FileOp.wFunc = FO_DELETE;
   FileOp.pFrom = sPath + vbNullChar;
   FileOp.pTo = vbNullChar;
   FileOp.fFlags = FOF_NOCONFIRMATION | FOF_ALLOWUNDO;
   FileOp.lpszProgressTitle = "Sending " + sPath + " to the Recycle Bin";
   try {
     SHFileOperation(FileOp);
   } catch (Exception ex) {
     MsgBox(ex.Message);
   }
   Recycle = 0;
 }
}
0
 

Author Comment

by:ba272
ID: 13763178
Thanks.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question