移动目录时发生的错误(20分)

  • 主题发起人 花牛007
  • 开始时间

花牛007

Unregistered / Unconfirmed
GUEST, unregistred user!
我编的以下程序,在运行第二个FileOp发生错误,我百思不解,请指教。

procedure Test;
begin
FileOp(handle,'d:/mydir1','d:/aaa','MOVE');
FileOp(handle,'d:/aaa/mydir1','d:/aaa','MOVE');
end;


procedure FileOp(hWndOwner: HWND; const SourceFile, TargetFile,strOp: string);
var sfosFile: TSHFileOpStruct;
begin
with sfosFile do
begin
Wnd := hWndOwner;
if strOP='COPY' then
wFunc := FO_COPY
else if strOP='MOVE' then
wFunc:=FO_MOVE
else if strOP='DELETE' then
wFunc:=FO_DELETE
else if strOP='RENAME' then
wFunc:=FO_RENAME
else
exit;
pFrom := pChar(SourceFile);
if strOP<>'DELETE' then
pTo := pChar(TargetFile)
else
pTo := #0#0;
fFlags := 0;
fAnyOperationsAborted := false;
end;
try
SHFileOperation(Info);
finally
showmessage(strOP+'错误!');
end;
end;
 
已经有目录 D:/aaa 怎么能再建立文件 D:/aaa 呢?
 
我换成
procedure Test;
begin
FileOp(handle,'d:/mydir1','d:/aaa','MOVE');
FileOp(handle,'d:/aaa/mydir1','d:/','MOVE');
end;

也是同样错误
 
procedure Test;
begin
FileOp(handle,'d:/mydir1','d:/aaa','MOVE');
FileOp(handle,'d:/aaa/mydir1','d:/aaa','MOVE');
end;


procedure FileOp(hWndOwner: HWND; const SourceFile, TargetFile,strOp: string);
var sfosFile: TSHFileOpStruct;
begin
with sfosFile do
begin
Wnd := hWndOwner;
if strOP='COPY' then
wFunc := FO_COPY
else if strOP='MOVE' then
wFunc:=FO_MOVE
else if strOP='DELETE' then
wFunc:=FO_DELETE
else if strOP='RENAME' then
wFunc:=FO_RENAME
else
exit;
pFrom := pChar(SourceFile);
if strOP<>'DELETE' then
pTo := pChar(TargetFile)
else
pTo := #0#0;
fFlags := 0;
fAnyOperationsAborted := false;
end;
try
SHFileOperation(Info);
// finally
//无论是否出错,你都会显示出错,因为是Finally而不是Except
//我的主页上有一篇关于Delphi异常的讲座,你去看看吧:http://www.aidelphi.com
这里改成Except吧,不过可能并不能栏截错误.
showmessage(strOP+'错误!');
end;
end;
 
楼上说得对,Finally 后的总是会执行。
 
我改成
Except
ShowMessage('错误')
后,同样的错误还是出现
 
1 这里的关键问题是要在文件(夹)名后加上两个连续的 0 。
如果不这样做,把 d:/aaa改成 d:/aaa1 虽然成功了,
但改回去就会出错。
2 我的 fFlags是如下设置的,这可能不影响。
fFlags := FOF_SILENT + FOF_NOCONFIRMATION ;
3 你贴出的代码可能下一行贴错了:
SHFileOperation(Info); 应为如下:
SHFileOperation(sfosfile);

4 最好在移动前先进行“文件夹是否存在”的检查,我想你会的。
5 以下是我调试成功的代码,不好意思,主要是你的代码:


procedure FileOp(hWndOwner: HWND; const SourceFile, TargetFile,strOp: string);
var sfosFile: TSHFileOpStruct;
begin
with sfosFile do
begin
Wnd := hWndOwner;
if strOP='COPY' then
wFunc := FO_COPY
else if strOP='MOVE' then
wFunc:=FO_MOVE
else if strOP='DELETE' then
wFunc:=FO_DELETE
else if strOP='RENAME' then
wFunc:=FO_RENAME
else
exit;
pFrom := pChar(SourceFile);
if strOP<>'DELETE' then
pTo := pChar(TargetFile)
else
pTo := #0#0;
fFlags := FOF_SILENT + FOF_NOCONFIRMATION ; //////////
fAnyOperationsAborted := false;
end;
try
SHFileOperation(sfosfile); //此行失败时不会引发意外,应自己检查错误引发
// sfosfile ,not Info.
except
showmessage(strOP+'错误!');
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
FileOp(handle,'d:/aaa1'+#0#0,'d:/aaa'+#0#0,'MOVE'); //////
FileOp(handle,'d:/bbb1'+#0#0,'d:/bbb2/bbb3'+#0#0,'MOVE'); //////
end;
 
procedure Test;
begin
FileOp(handle,'d:/mydir1','d:/aaa','MOVE');
FileOp(handle,'d:/aaa/mydir1','d:/aaa','MOVE');
end;


procedure FileOp(hWndOwner: HWND; const SourceFile, TargetFile,strOp: string);
var sfosFile: TSHFileOpStruct;
begin
with sfosFile do
begin
Wnd := hWndOwner;
if strOP='COPY' then
wFunc := FO_COPY
else if strOP='MOVE' then
wFunc:=FO_MOVE
else if strOP='DELETE' then
wFunc:=FO_DELETE
else if strOP='RENAME' then
wFunc:=FO_RENAME
else
exit;
pFrom := pChar(SourceFile);
if strOP<>'DELETE' then
pTo := pChar(TargetFile)
else
pTo := #0#0;
fFlags := 0;
fAnyOperationsAborted := false;
end;
try
SHFileOperation(sfosFile);
//楼上没了,这里的参数有问题!应该是sfosFile
Except
showmessage(strOP+'错误!');
end;
end;
 
谢谢各位的参与,aizb与jsxjd两位仁兄辛苦了。
这种错误可能是系统的缘故。
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
532
import
I
I
回复
0
查看
419
import
I
I
回复
0
查看
658
import
I
I
回复
0
查看
733
import
I
顶部