主题:[讨论]用SHFileOperation删除目录遇到的奇怪问题,大家进来研究下
下面是代码一,可以正常删除'c:\kk'目录
procedure TFrmMain.Button2Click(Sender: TObject);
var
fileop:SHFILEOPSTRUCT;
begin
fileop.Wnd:=Application.handle;
fileop.wFunc:=FO_DELETE;
fileOp.pFrom:=Pchar('c:\kk'+#0);
fileOp.fFlags:=FOF_NOCONFIRMATION;
if SHFileOperation(fileop)<>0 then
showmessage('wrong')
else
showmessage('ok');
end;
代码二,不能正常运行,有错误“First chance exception at $7C812A5B. Exception class EAccessViolation with message 'Access violation at address 7C84C0D1 in module 'kernel32.dll'. Read of address 423F0012'. Process STS.exe (4016)”
procedure TFrmMain.Button2Click(Sender: TObject);
var
strDirForDelete:string;//多了这句
fileop:SHFILEOPSTRUCT;
begin
fileop.Wnd:=Application.handle;
fileop.wFunc:=FO_DELETE;
fileOp.pFrom:=Pchar('c:\kk'+#0);
fileOp.fFlags:=FOF_NOCONFIRMATION;
if SHFileOperation(fileop)<>0 then
showmessage('wrong')
else
showmessage('ok');
end;
代码三,我把它写成了一个专门的函数,但还是同样会出错
function TFrmMain.DelDirForce(strDirForDelete:string;blnCanRedo:boolean):boolean;
var
fileop:SHFILEOPSTRUCT;
begin
result := False;
if not DirectoryExists(strDirForDelete) then exit; //目录不存在就退出
fileop.Wnd:=Application.handle;
fileop.wFunc:=FO_DELETE;
fileOp.pFrom:=Pchar(strDirForDelete+#0+#0);
if blnCanRedo then
fileOp.fFlags:=FOF_ALLOWUNDO+FOF_NOCONFIRMATION
else
fileOp.fFlags:=FOF_NOCONFIRMATION;
if SHFileOperation(fileop)<>0 then
result := False
else
result := True;
end;
大伙给看看,到底是什么问题呢?上网查了很多资料都解决不了,郁闷极了![em10]
procedure TFrmMain.Button2Click(Sender: TObject);
var
fileop:SHFILEOPSTRUCT;
begin
fileop.Wnd:=Application.handle;
fileop.wFunc:=FO_DELETE;
fileOp.pFrom:=Pchar('c:\kk'+#0);
fileOp.fFlags:=FOF_NOCONFIRMATION;
if SHFileOperation(fileop)<>0 then
showmessage('wrong')
else
showmessage('ok');
end;
代码二,不能正常运行,有错误“First chance exception at $7C812A5B. Exception class EAccessViolation with message 'Access violation at address 7C84C0D1 in module 'kernel32.dll'. Read of address 423F0012'. Process STS.exe (4016)”
procedure TFrmMain.Button2Click(Sender: TObject);
var
strDirForDelete:string;//多了这句
fileop:SHFILEOPSTRUCT;
begin
fileop.Wnd:=Application.handle;
fileop.wFunc:=FO_DELETE;
fileOp.pFrom:=Pchar('c:\kk'+#0);
fileOp.fFlags:=FOF_NOCONFIRMATION;
if SHFileOperation(fileop)<>0 then
showmessage('wrong')
else
showmessage('ok');
end;
代码三,我把它写成了一个专门的函数,但还是同样会出错
function TFrmMain.DelDirForce(strDirForDelete:string;blnCanRedo:boolean):boolean;
var
fileop:SHFILEOPSTRUCT;
begin
result := False;
if not DirectoryExists(strDirForDelete) then exit; //目录不存在就退出
fileop.Wnd:=Application.handle;
fileop.wFunc:=FO_DELETE;
fileOp.pFrom:=Pchar(strDirForDelete+#0+#0);
if blnCanRedo then
fileOp.fFlags:=FOF_ALLOWUNDO+FOF_NOCONFIRMATION
else
fileOp.fFlags:=FOF_NOCONFIRMATION;
if SHFileOperation(fileop)<>0 then
result := False
else
result := True;
end;
大伙给看看,到底是什么问题呢?上网查了很多资料都解决不了,郁闷极了![em10]