Win32 APIのSHQueryRecycleBin関数を用いることで、ごみ箱にあるファイルのサイズと個数をドライブ毎に取得することができる。  LPSHQUERYRBINFOのcbSizeにはあらかじめ構造体の長さを入れておき、SHQueryRecycleBinを呼び出すとi64Size にはごみ箱にあるファイルの総容量、i64NumItemsにはごみ箱にあるファイルの個数が代入される。

pszRootPathには、対象のドライブを指定する。 C:\を指定すると、Cドライブのみのサイズ、個数が取得できる。 ちなみに、pszRootPathに""を指定すると全てのドライブ合計のファイル数・サイズが取得できるが、なぜかWindows 2000では正しく取得できない

Sponsored Link

Imports System
Imports System.Runtime.InteropServices

Namespace SantaMarta.Tips.RecycleBin

    Class QueryRecycleBin

        <DllImport("shell32.dll", CharSet:=CharSet.Auto)> _
        Friend Shared Function SHQueryRecycleBin( _
            ByVal pszRootPath As String, _
            <MarshalAs(UnmanagedType.Struct)> ByRef pSHQueryRBInfo As LPSHQUERYRBINFO) _
            As Integer
        End Function

        <StructLayout(LayoutKind.Sequential, Pack:=4)> _
        Friend Structure LPSHQUERYRBINFO

            Public cbSize As Integer
            Public i64Size As Long
            Public i64NumItems As Long

        End Structure

        Friend Const S_OK As Integer = 0

        ''' <summary>
        ''' アプリケーションのメイン エントリ ポイントです。
        ''' </summary>
        Shared Sub Main(ByVal args As String())

            Dim info As LPSHQUERYRBINFO
            Dim result As Integer

            For Each drive As String In Environment.GetLogicalDrives()

                info = New LPSHQUERYRBINFO
                info.cbSize = Marshal.SizeOf(info)

                result = SHQueryRecycleBin(drive, info)

                Console.Write("{0}ドライブ / ", drive.Chars(0))

                If result = S_OK Then

                    Console.WriteLine("ファイル: {0:0} 個、サイズ: {1:###,###,##0} Bytes", info.i64NumItems, info.i64Size)

                Else

                    Console.WriteLine("取得できませんでした。")

                End If

            Next

        End Sub

    End Class

End Namespace
 _
        Friend Shared Function SHQueryRecycleBin( _
            ByVal pszRootPath As String, _
             ByRef pSHQueryRBInfo As LPSHQUERYRBINFO) _
            As Integer
        End Function

         _
        Friend Structure LPSHQUERYRBINFO

            Public cbSize As Integer
            Public i64Size As Long
            Public i64NumItems As Long

        End Structure

        Friend Const S_OK As Integer = 0

        ''' 
        ''' アプリケーションのメイン エントリ ポイントです。
        ''' 
        Shared Sub Main(ByVal args As String())

            Dim info As LPSHQUERYRBINFO
            Dim result As Integer

            For Each drive As String In Environment.GetLogicalDrives()

                info = New LPSHQUERYRBINFO
                info.cbSize = Marshal.SizeOf(info)

                result = SHQueryRecycleBin(drive, info)

                Console.Write("{0}ドライブ / ", drive.Chars(0))

                If result = S_OK Then

                    Console.WriteLine("ファイル: {0:0} 個、サイズ: {1:###,###,##0} Bytes", info.i64NumItems, info.i64Size)

                Else

                    Console.WriteLine("取得できませんでした。")

                End If

            Next

        End Sub

    End Class

End Namespace]]>
using System;
using System.Runtime.InteropServices;

namespace SantaMarta.Tips.RecycleBin
{
    class QueryRecycleBin
    {
        [DllImport( "shell32.dll", CharSet = CharSet.Auto )]
        internal static extern uint SHQueryRecycleBin(
            string pszRootPath,
            [MarshalAs( UnmanagedType.Struct )]ref LPSHQUERYRBINFO pSHQueryRBInfo );

        [StructLayout( LayoutKind.Sequential, Pack = 4 )]
        internal struct LPSHQUERYRBINFO
        {
            public uint cbSize;
            public ulong i64Size;
            public ulong i64NumItems;
        }

        internal const uint S_OK = 0;

        /// <summary>
        /// アプリケーションのメイン エントリ ポイントです。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            LPSHQUERYRBINFO info;
            uint result;
    
            foreach ( string drive in Environment.GetLogicalDrives() )
            {
                info = new LPSHQUERYRBINFO();
                info.cbSize = (uint)Marshal.SizeOf( info );

                result = SHQueryRecycleBin( drive, ref info );

                Console.Write( "{0}ドライブ / ", drive[0] );

                if ( result == S_OK ) 
                {
                    Console.WriteLine( "ファイル: {0:0} 個、サイズ: {1:###,###,##0} Bytes", info.i64NumItems, info.i64Size );
                }
                else
                {
                    Console.WriteLine( "取得できませんでした。" );
                }
            }
        }
    }
}

        /// アプリケーションのメイン エントリ ポイントです。
        /// 
        [STAThread]
        static void Main(string[] args)
        {
            LPSHQUERYRBINFO info;
            uint result;
    
            foreach ( string drive in Environment.GetLogicalDrives() )
            {
                info = new LPSHQUERYRBINFO();
                info.cbSize = (uint)Marshal.SizeOf( info );

                result = SHQueryRecycleBin( drive, ref info );

                Console.Write( "{0}ドライブ / ", drive[0] );

                if ( result == S_OK ) 
                {
                    Console.WriteLine( "ファイル: {0:0} 個、サイズ: {1:###,###,##0} Bytes", info.i64NumItems, info.i64Size );
                }
                else
                {
                    Console.WriteLine( "取得できませんでした。" );
                }
            }
        }
    }
}]]>
出力例
Aドライブ / ファイル: 0 個、サイズ: 0 Bytes
Cドライブ / ファイル: 1 個、サイズ: 137 Bytes
Dドライブ / ファイル: 2 個、サイズ: 2,279 Bytes
Eドライブ / ファイル: 1 個、サイズ: 138 Bytes
Fドライブ / ファイル: 0 個、サイズ: 0 Bytes
Gドライブ / ファイル: 0 個、サイズ: 0 Bytes
Hドライブ / ファイル: 0 個、サイズ: 0 Bytes
Iドライブ / ファイル: 0 個、サイズ: 0 Bytes
Jドライブ / ファイル: 0 個、サイズ: 0 Bytes
Press any key to continue