blog

Home / DeveloperSection / Blogs / Recycle bin in c#.

Recycle bin in c#.

Anonymous User8361 25-May-2011

In this article I will describe that how to work with recycle bin such as retrieving number of files in recycle bin, calculating size of files in recycle bin and deleting content of recycle bin. First of all I would like to tell you that I had already solve it by adding reference of Shell32 dll but that is not great solution because when I create setup then due to Shell32 dll size of application become more than 6 mb which is not good. So finally lot of googling and forums search I got an efficient solution which worked for me. Here I will give you sample code which is useful for you.

Sample code for recycle bin
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace ConsoleApplication5
{
    class Program
    {
        /// <summary>
        /// Create an structure which will store recycle query information.
        /// </summary>
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 1)]
        public struct SHQUERYRBINFO
        {
            public Int32 cbSize;
            public UInt64 i64Size;
            public UInt64 i64NumItems;
        }
        /// <summary>
        /// Call SHQueryRecycleBin() method of shell32 dll to query file size and file number
        /// in recycle bin.
        /// </summary>
        [DllImport("shell32.dll", CharSet = CharSet.Unicode)]
        public static extern int SHQueryRecycleBin(
                [MarshalAs(UnmanagedType.LPTStr)]
                String pszRootPath,
                ref SHQUERYRBINFO pSHQueryRBInfo
            );
        enum RecycleFlags : int
        {
            SHERB_NOCONFIRMATION = 0x00000001,          //No confirmation dialog will open while emptying recycle bin.
            SHERB_NOPROGRESSUI = 0x00000001,            //No progress tracking window appears while emptying recycle bin.
            SHERB_NOSOUND = 0x00000004                  //No sound whent while emptying recycle bin.
        }
        /// <summary>
        /// Call SHEmptyRecycleBin() method of shell32 dll to empty recycle bin.
        /// </summary>
        [System.Runtime.InteropServices.DllImport("Shell32.dll")]
        static extern int SHEmptyRecycleBin(IntPtr hwnd, string psrRootPath, RecycleFlags dwFlags);
        /// <summary>
        /// This method will empty recycle bin without prompting confirmation message.
        /// </summary>
        public static void emptyRecycleBin()
        {
            SHEmptyRecycleBin(IntPtr.Zero, null, RecycleFlags.SHERB_NOCONFIRMATION);           
        }
        static void Main(string[] args)
        {
            SHQUERYRBINFO bb_Query = new SHQUERYRBINFO();
            bb_Query.cbSize = Marshal.SizeOf(bb_Query.GetType());
            SHQueryRecycleBin(null, ref bb_Query);
            Console.WriteLine("CB Size  :  " + bb_Query.cbSize);
            Console.WriteLine("File Size   :  " + bb_Query.i64Size);  //Call i64Size member of structure which will return size of recycle bin.
            Console.WriteLine("Number of items   :  " + bb_Query.i64NumItems);  //Call i64NumItems member of structure which will return file number in recycle bin.
            emptyRecycleBin();   //call empty recycle bin which will empty ur recycle bin.
        }
    }
}


Updated 18-Sep-2014
I am a content writter !

Leave Comment

Comments

Liked By