Jump to content

[C#] Get the recycle bin size and file count with C# and Win32 API

- - - - -

  • Please log in to reply
1 reply to this topic

#1
PsychoCoder

PsychoCoder

    Learning Programmer

  • Members
  • PipPipPip
  • 92 posts
/*Need to import the following Namespaces

System.ComponentModel;

System.Runtime.InteropServices

System.Collections.Generic*/


/*Need this struct

Need the SHQUERYRBINFO struct*/


//First the SHQUERYRBINFO struct

/// <summary>

/// struct representing the SHQUERYRBINFO structure

/// </summary>

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack=1)]

public struct SHQUERYRBINFO

{

    public Int32 cbSize;

    public UInt64 i64Size;

    public UInt64 i64NumItems;

}

 

//Now the Win32 API

[DllImport("shell32.dll", CharSet = CharSet.Unicode)]

public static extern int SHQueryRecycleBin(string pszRootPath, ref SHQUERYRBINFO pSHQueryRBInfo);

 

//Now the method that does the work

/// <summary>

/// method for getting total files in the recycle bin and it's overall size

/// </summary>

/// <returns></returns>

public List<string> GetRecycleBinSize()

{

    SHQUERYRBINFO query = new SHQUERYRBINFO();

    List<string> info = new List<string>();

    query.cbSize = Marshal.SizeOf(typeof(SHQUERYRBINFO));

 

    try

    {

        int result = SHQueryRecycleBin(null, ref query);

 

        if (result == 0)

        {

            info.Add(query.i64NumItems.ToString());

            info.Add(string.Format("{0}", (Convert.ToDouble(query.i64Size) / Convert.ToDouble(1024) / Convert.ToDouble(1024)).ToString("#,###.##")));

 

            return info;

        }

        else

            throw new Win32Exception(Marshal.GetLastWin32Error());

    }

    catch (Exception ex)

    {

        MessageBox.Show(string.Format("Error accessing Recycle Bin: {0}", ex.Message), "System Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

        return null;

    }

 

}


#2
awadhendratiwari

awadhendratiwari

    Newbie

  • Members
  • Pip
  • 1 posts
Hi....
Please take a look at this blog.
In this blog I will manage recycle bin in c# such as retriving size of file of recycle bin , file number and empting recycle bin.
Recycle bin in c#.
Please take a look at here.
This might be useful for you.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users