ごみ箱にあるファイルのサイズと個数を取得する

注意:
この文書は以前「.NETでいきまっしょい!」で公開していたものですが、公開以降メンテナンスされていません。 今や古い情報となった内容が記載されている場合があるのでご注意ください。

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

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

VB.NET
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
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
C#
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
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( "取得できませんでした。" );
                }
            }
        }
    }
}
出力例
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