Thursday, 6 March 2008

File && || folder deleting in C#

Recently, I´ve been playing around with file and folder deleting from C# applications. I´ve seen two issues very easy to workaround that might help you.

1.- System.Drawing.Bitmap.FromFile() locks the file used.

If you try to delete a folder containing a bitmap you used in that manner, it will fail, as it´ll be locked by your application. In order to avoid this behavior, you can just clone the bitmap an dispose the original. Just like this:


Bitmap tempBitmap = (Bitmap)Bitmap.FromFile(filename);
mStoredBitmap = new Bitmap(tempBitmap);
tempBitmap.Dispose();

Of course, there are many ways to fix this. This one clones the bitmap from memory so the file can be unlocked in the dispose.

2.- How to delete a file or folder sending it to the recycle bin?

This is a pretty obvious need and, at first sight, there´s no direct support for that in plain C#. In this link, you will find some information about this.

Among the solutions offered there, there´s a very easy and direct one (if you don´t mind to add a reference to Microsoft.VisualBasic dll in yor application).

Just add that reference, and instead of System.IO.Directory.Delete(), use this one:

Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory
(
filename or folder,
Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs,
Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin
);

Cheers!

2 comments:

Khariano said...
This comment has been removed by the author.
Khariano said...

Very useful. Thank you!