کد:
//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;
}
}