1. Home
  2. Computing & Technology
  3. Visual Basic
photo of Dan Mabbutt
Dan's Visual Basic Blog

By Dan Mabbutt, About.com Guide to Visual Basic since 2002

Recycle Files With VB.NET 2005

Friday December 22, 2006

Another Reason To Move Up to VB.NET 2005

Most VB programmers know about the various ways to delete a file that have always been available in Visual Basic. There's the Kill function in VB 6 and VB.NET. The FileSystemObject in VB 6 does the trick. The File and FileInfo objects in VB.NET delete files. But there's one thing that none of these can do and that is to delete files into the Recycle bin.

Your hard disk contains a hidden folder named Recycled (Recycler in NTFS) that contains files deleted in Windows Explorer, My Computer or Windows-based programs. When you delete a file, the complete path and file name is stored in a hidden file called Info and the deleted file is renamed. So you can't just move a file to recycle it.

In the past, you had to use the SHFileOperation Win32 API to delete a file and place it in the Recycle Bin. (MSDN has instructions showing how to do that if you need them.) But one of the functions that was added with the My namespace in VB.NET 2005 was the ability to move files easily into the Recycle Bin.

Simply code ...

My.Computer.FileSystem.DeleteFile(<path string>, _
	<display dialog option>, _
	 FileIO.RecycleOption.SendToRecycleBin)

The "display dialog option" even gives you the ability to show those handy windows to confirm the file deletion and display errors if something goes wrong. What could be easier?

Now ... if only there was an "undelete" option too. I haven't found one of those. Any help?

Comments

December 25, 2006 at 11:41 am
(1) Mike says:

I’m not sure if an undelete option exists. I don’t think I’ve used a microsoft program that had one, and I’d think they’d know the most about their API’s. Usually once you delete something the undo option disappears in Office programs for example.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.