一个递归的算法复制文件,为啥会蓝屏?(80分)

X

xgpxf

Unregistered / Unconfirmed
GUEST, unregistred user!
程序如下:
procedure CopyFiles(FromDir, ToDir: string; ShowProgress: Boolean);
var
; srSource, srTo: TSearchRec;
; PathStr: string;
; Attr, rnoSource: Integer;
; fDate, sDate: string;
; ItemStr: string;
begin
; try
; ; Attr := faAnyFile;
; ; try
; ; ; if not DirectoryExists(FromDir) then
; ; ; begin
; ; ; ; Application.MessageBox('The Directory not found !', 'Error', MB_OK + MB_ICONERROR);
; ; ; ; Exit;
; ; ; end;
; ; ; rnoSource := FindFirst(FromDir + '*.*', Attr, srSource);
; ; ; while rnoSource = 0 do
; ; ; begin
; ; ; ; if ShowProgress then if frmProgress.ToStop then Break;
; ; ; ; if ((srSource.Attr and faDirectory) = faDirectory) and (srSource.Name <> '.') and (srSource.Name<>'..') then
; ; ; ; begin
; ; ; ; ; PathStr := FromDir + srSource.Name;
; ; ; ; ; if not DirectoryExists(ToDir + srSource.Name) then
; ; ; ; ; ; if not CreateDir(ToDir + srSource.Name) then
; ; ; ; ; ; ; raise Exception.Create('Cannot create ' + ToDir + srSource.Name);
; ; ; ; ; CopyFiles(AddLastBackslash(PathStr), AddLastBackslash(ToDir + srSource.Name), ShowProgress);
; ; ; ; ; // AddLastBackslash 是一个加'/'的过程。
; ; ; ; end;
; ; ; ; if ((srSource.Attr and faDirectory) <> faDirectory) then
; ; ; ; begin
; ; ; ; ; if FileExists(ToDir + srSource.Name) then
; ; ; ; ; begin
; ; ; ; ; ; if FindFirst(ToDir + srSource.Name, faAnyFile, srTo) = 0 then
; ; ; ; ; ; try
; ; ; ; ; ; ; fDate := DateTimeToStr(FileDateToDateTime(srSource.Time));
; ; ; ; ; ; ; sDate := DateTimeToStr(FileDateToDateTime(srTo.Time));
; ; ; ; ; ; ; if ShowProgress then frmprogress.SetText('Checking ' + srSource.Name);
; ; ; ; ; ; ; if srSource.Time <> srTo.Time then
; ; ; ; ; ; ; begin
; ; ; ; ; ; ; ; if ShowProgress then frmProgress.SetText('Copying ' + srSource.Name);
; ; ; ; ; ; ; ; if CopyFile(pchar(FromDir + srSource.Name), pchar(ToDir + srSource.Name), False) then
; ; ; ; ; ; ; ; begin
; ; ; ; ; ; ; ; ; ItemStr := 'Copy: ' + FromDir + SrSource.Name + ' ;To: ' + ToDir + srSource.Name;
; ; ; ; ; ; ; ; ; if ShowProgress then frmProgress.Memo.Lines.Add(ItemStr);
; ; ; ; ; ; ; ; end else
; ; ; ; ; ; ; ; begin
; ; ; ; ; ; ; ; ; ItemStr := 'Copy ' + FromDir + srSource.Name + ' failed';
; ; ; ; ; ; ; ; ; if ShowProgress then frmProgress.Memo.Lines.Add(ItemStr);
; ; ; ; ; ; ; ; end;
; ; ; ; ; ; ; ; Application.ProcessMessages;
; ; ; ; ; ; ; end;
; ; ; ; ; ; finally
; ; ; ; ; ; ; FindClose(srTo);
; ; ; ; ; ; end;
; ; ; ; ; end
; ; ; ; ; else begin
; ; ; ; ; ; if ShowProgress then frmProgress.SetText('Copying ' + srSource.Name);
; ; ; ; ; ; if CopyFile(pchar(FromDir + srSource.name), pchar(ToDir + srSource.name), False) then
; ; ; ; ; ; begin
; ; ; ; ; ; ; ItemStr := 'Copy: ' + FromDir + SrSource.Name + ' ;To: ' + ToDir + srSource.Name;
; ; ; ; ; ; ; if ShowProgress then frmProgress.Memo.Lines.Add(ItemStr);
; ; ; ; ; ; end else
; ; ; ; ; ; begin
; ; ; ; ; ; ; ItemStr := 'Copy ' + FromDir + srSource.Name + ' failed';
; ; ; ; ; ; ; if ShowProgress then frmProgress.Memo.Lines.Add(ItemStr);
; ; ; ; ; ; end;
; ; ; ; ; ; Application.ProcessMessages;
; ; ; ; ; end;
; ; ; ; end;
; ; ; ; rnoSource:=FindNext(srSource);
; ; ; end;
; ; finally
; ; ; FindClose(srSource);
; ; end;
; ; if ShowProgress then frmProgress.SetText('Completed !');
; except
; ; raise Exception.create('Error');
; end;
end;

自己试是可以的,但别人用说会蓝屏,帮忙看看。
 
我试过,除了没使用frmProgress外,其余一切正常。
环境:delphi6,windows me.
 
if not Assigned(frmProgress) then frmProgress := TfrmProgress.Create(Application);
frmProgress.Init; //自己写的一个初始化过程。
frmProgress.Memo.Lines.Clear;
frmProgress.Show;
try
; CopyFiles(AddLastBackslash(edtfrom.Text), AddLastBackslash(edtTo.Text), True);
finally
; frmProgress.btnClose.Visible := True;
; frmProgress.btnCancel.Visible := False;
; if CloseProcess then frmProgress.Close;
end;
我是先建立frmProgres后在调用copyfiles递归的。我想这个是没问题的,所以开始没写出来。

整个程序我试过N遍了,都没问题,但我给另外一个人(在香港,没法去调试)用他就说会蓝屏,我很奇怪。
 
如果不是操作系统的问题,就是那个人的问题了。
 
可能他目录层次太多,递归次数太多,引起堆栈溢出了吧
 
接受答案了.
 
顶部