Como verificar se a lixeira está vazia?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
| { É necessário estar declarado SysUtils e Windows na seção uses.
A partir das versões unicode declare System.Sysutils e
WinApi.Windows. }
function RecycleBinInfo(const Drive: Char; out BinSize, FileCount: Int64):
Boolean;
type
// structure passed to SHQueryRecycleBin to get information about recyle bin
TSHQueryRBInfo = packed record
cbSize: Integer; // size of structure
i64Size: Int64; // size of recycle bin
i64NumItems: Int64; // number of items in recycle bin.
end;
PSHQueryRBInfo = ^TSHQueryRBInfo;
// function prototype for SHQueryRecycleBin (prototypes Unicode version when
// PChar = PWideChar and ANSI version when PChar = PAnsiChar
TSHQueryRecycleBin = function(RootPath: PChar; Query: PSHQueryRBInfo):
Integer; stdcall;
const
{$IFDEF UNICODE}
cSHQueryRecycleBin = 'SHQueryRecycleBinW'; // Unicode SHQueryRecycleBin fn
{$ELSE}
cSHQueryRecycleBin = 'SHQueryRecycleBinA'; // ANSI SHQueryRecycleBin fn
{$ENDIF}
cShell32 = 'shell32.dll'; // SHQueryRecycleBin's DLL
var
SHQueryRecycleBin: TSHQueryRecycleBin; // reference to SHQueryRecycleBin fn
Shell32: THandle; // handle to shel32.dll
SHQueryRBInfo: TSHQueryRBInfo; // structure receive recyle bin info
RootDir: string; // directory we are getting info for
PRootDir: PChar; // pointer to root dir or nil
begin
// Intialise to assume failure
Result := False;
BinSize := 0;
FileCount := 0;
// Attempt to load required SHQueryRecycleBin function from DLL
Shell32 := SafeLoadLibrary(cShell32);
if (Shell32 <> 0) then
begin
@SHQueryRecycleBin := GetProcAddress(Shell32,
cSHQueryRecycleBin);
if Assigned(@SHQueryRecycleBin) then
begin
// Set structure size before calling SHQueryRecycleBin
SHQueryRBInfo.cbSize := SizeOf(SHQueryRBInfo);
// Create pointer to required drive (nul to get all drives)
{$IFDEF UNICODE}
if CharInSet(UpCase(Drive), ['A'..'Z']) then
{$ELSE}
if UpCase(Drive) in ['A'..'Z'] then
{$ENDIF}
begin
RootDir := UpCase(Drive) + ':\';
PRootDir := PChar(RootDir);
end
else
PRootDir := nil;
// Get recycle info
Result := SHQueryRecycleBin(PRootDir, @SHQueryRBInfo) = 0;
if (Result) then
begin
// Success: pass info back through params
BinSize := SHQueryRBInfo.i64Size;
FileCount := SHQueryRBInfo.i64NumItems;
end;
end;
end;
end;
// Exemplo de uso
procedure TForm1.Button1Click(Sender: TObject);
var
Binsize, FileCount : Int64;
begin
// Verificando a lixeira do disco C.
RecycleBinInfo('C', BinSize, FileCount);
// Exibindo o total de arquivos.
ShowMessage('Total de arquivos: ' + IntToStr(FileCount));
// Exibindo o total de arquivos.
ShowMessage('Total de Bits: ' + IntToStr(Binsize));
if FileCount = 0 then
ShowMessage('A Lixeira está vazia.')
else
ShowMessage('A Lixeira não está vazia.');
end; |
{ É necessário estar declarado SysUtils e Windows na seção uses.
A partir das versões unicode declare System.Sysutils e
WinApi.Windows. }
function RecycleBinInfo(const Drive: Char; out BinSize, FileCount: Int64):
Boolean;
type
// structure passed to SHQueryRecycleBin to get information about recyle bin
TSHQueryRBInfo = packed record
cbSize: Integer; // size of structure
i64Size: Int64; // size of recycle bin
i64NumItems: Int64; // number of items in recycle bin.
end;
PSHQueryRBInfo = ^TSHQueryRBInfo;
// function prototype for SHQueryRecycleBin (prototypes Unicode version when
// PChar = PWideChar and ANSI version when PChar = PAnsiChar
TSHQueryRecycleBin = function(RootPath: PChar; Query: PSHQueryRBInfo):
Integer; stdcall;
const
{$IFDEF UNICODE}
cSHQueryRecycleBin = 'SHQueryRecycleBinW'; // Unicode SHQueryRecycleBin fn
{$ELSE}
cSHQueryRecycleBin = 'SHQueryRecycleBinA'; // ANSI SHQueryRecycleBin fn
{$ENDIF}
cShell32 = 'shell32.dll'; // SHQueryRecycleBin's DLL
var
SHQueryRecycleBin: TSHQueryRecycleBin; // reference to SHQueryRecycleBin fn
Shell32: THandle; // handle to shel32.dll
SHQueryRBInfo: TSHQueryRBInfo; // structure receive recyle bin info
RootDir: string; // directory we are getting info for
PRootDir: PChar; // pointer to root dir or nil
begin
// Intialise to assume failure
Result := False;
BinSize := 0;
FileCount := 0;
// Attempt to load required SHQueryRecycleBin function from DLL
Shell32 := SafeLoadLibrary(cShell32);
if (Shell32 <> 0) then
begin
@SHQueryRecycleBin := GetProcAddress(Shell32,
cSHQueryRecycleBin);
if Assigned(@SHQueryRecycleBin) then
begin
// Set structure size before calling SHQueryRecycleBin
SHQueryRBInfo.cbSize := SizeOf(SHQueryRBInfo);
// Create pointer to required drive (nul to get all drives)
{$IFDEF UNICODE}
if CharInSet(UpCase(Drive), ['A'..'Z']) then
{$ELSE}
if UpCase(Drive) in ['A'..'Z'] then
{$ENDIF}
begin
RootDir := UpCase(Drive) + ':\';
PRootDir := PChar(RootDir);
end
else
PRootDir := nil;
// Get recycle info
Result := SHQueryRecycleBin(PRootDir, @SHQueryRBInfo) = 0;
if (Result) then
begin
// Success: pass info back through params
BinSize := SHQueryRBInfo.i64Size;
FileCount := SHQueryRBInfo.i64NumItems;
end;
end;
end;
end;
// Exemplo de uso
procedure TForm1.Button1Click(Sender: TObject);
var
Binsize, FileCount : Int64;
begin
// Verificando a lixeira do disco C.
RecycleBinInfo('C', BinSize, FileCount);
// Exibindo o total de arquivos.
ShowMessage('Total de arquivos: ' + IntToStr(FileCount));
// Exibindo o total de arquivos.
ShowMessage('Total de Bits: ' + IntToStr(Binsize));
if FileCount = 0 then
ShowMessage('A Lixeira está vazia.')
else
ShowMessage('A Lixeira não está vazia.');
end;
Baseado na dica do site: http://www.experts-exchange.com/
-
InfusTec
-
1.527 views
- 0 comentários
- 14 de março de 2015
Está gostando do conteúdo? Considere pagar um cafezinho para nossa equipe!
Posts Relacionados - Continue Aprendendo