• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

Sending a file to the recycling bin

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
ba272
Asked:
ba272
  • 2
2 Solutions
 
AlexFMCommented:
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
 
beryl666Commented:
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
 
ba272Author Commented:
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now