异常处理无法屏蔽CopyFile吗?(100分)

  • 主题发起人 主题发起人 liuchong
  • 开始时间 开始时间
L

liuchong

Unregistered / Unconfirmed
GUEST, unregistred user!

try
.....
except
end;
结构处理CopyFile,可是把程序放在u盘中(u盘为写保护的时候),还是会出现错误提示
请教如何屏蔽错误提示。
出错部分代码:
try
FileName:=FilePath+ExtractFileName(FileList);
CopyFile(PChar(FileList),PChar(FileName),False);
FileName:=ExtractFileName(FileList)+#13#10;
CompleteFile.WriteBuffer(FileName[1],Length(FileName));
except
end;




完整源码:
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
procedure MyException(Sender:TObject;E:Exception);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

function GetDrivers():TStringList;
var
Drivers:PChar;
Driver:string;
BufferLength,I:Cardinal;
begin
Result:=TStringList.Create;
BufferLength:=GetLogicalDriveStrings(0,nil);
Drivers:=AllocMem(BufferLength);
GetLogicalDriveStrings(BufferLength,Drivers);
Driver:='';
for I:=0 to BufferLength-1 do
begin
if Drivers<>#0 then
Driver:=Driver+Drivers
else
begin
if Driver<>'' then
Result.Append(Driver);
Driver:='';
end;
end;
FreeMem(Drivers);
end;

function GetDriverType(Driver:PChar):string;
begin
case GetDriveType(Driver) of
2:Result:='可移动驱动器';
3:Result:='固定驱动器';
4:Result:='网络驱动器';
5:Result:='CD-ROM驱动器';
6:Result:='虚拟驱动器';
else
Result:='无效';
end;
end;

//递归查找文件:
procedure RecurSearchFile(CurrentDir:string;SearchFileType:string;SearchResult:TStrings;var Number:Integer);
var
I:Integer;
SubDir:TStringList;
SearchRec:TSearchRec;
begin
//第一次调用FindFirst和FindNext查找符合要求的文件
if (FindFirst(CurrentDir+SearchFileType,faAnyFile,SearchRec)=0) then
begin
repeat
if FileExists(CurrentDir+SearchRec.Name) and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
begin
Inc(Number);
Searchresult.Add(CurrentDir+SearchRec.Name);
end;
Application.ProcessMessages ;
until (FindNext(SearchRec)<>0);
end;
FindClose(SearchRec);

//以下是递归部分,查找各子目录。
SubDir:=TStringList.Create;
if (FindFirst(CurrentDir+'*.*',faAnyFile,SearchRec)=0) then
begin
repeat
if DirectoryExists(CurrentDir+SearchRec.Name) and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
SubDir.Add(SearchRec.Name);
Application.ProcessMessages ;
until (FindNext(SearchRec)<>0);
end;
FindClose(SearchRec);
for I:=0 to SubDir.Count-1 do
RecurSearchFile(CurrentDir+SubDir.Strings+'/',SearchFileType,SearchResult,Number);
//资源释放并返回结果。
FreeAndNil(SubDir);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
DriverList,FileList:TStringList;
I,FileNumber:Integer;
FilePath,FileName:string;
CompleteFile:TFileStream;
begin
Application.OnException:=MyException;
FilePath:=ExtractFilePath(ParamStr(0))+'MyCopy/';
ForceDirectories(FilePath);
I:=0;
FileList:=TStringList.Create;
CompleteFile:=TFileStream.Create(FilePath+'CompleteFile.txt',fmCreate);
try
//取硬盘盘符列表:
DriverList:=GetDrivers;
while I<=DriverList.Count-1 do
begin
if (DriverList='C:/') or (GetDriverType(PChar(DriverList))<>'固定驱动器') then
begin
DriverList.Delete(I);
Continue;
end;
Inc(I);
end;
//取文件列表:
for I:=0 to (DriverList.Count-1) do
RecurSearchFile(DriverList,'*.tif',FileList,FileNumber);
FileList.SaveToFile(FilePath+'FileList.txt');
//拷贝文件:
if FileList.Count>0 then
for I:=0 to (FileList.Count-1) do
begin
try
FileName:=FilePath+ExtractFileName(FileList);
CopyFile(PChar(FileList),PChar(FileName),False);
FileName:=ExtractFileName(FileList)+#13#10;
CompleteFile.WriteBuffer(FileName[1],Length(FileName));
except
end;
end;
FileName:='<<<全部完成>>>';
CompleteFile.WriteBuffer(FileName[1],Length(FileName));
finally
FreeAndNil(DriverList);
FreeAndNil(FileList);
FreeAndNil(CompleteFile);
end;
Application.Terminate;
end;

procedure TForm1.MyException(Sender: TObject
E: Exception);
begin

end;
 
判断是否写保护不就行了。
 
try
FileName:=FilePath+ExtractFileName(FileList);
CopyFile(PChar(FileList),PChar(FileName),False);
FileName:=ExtractFileName(FileList)+#13#10;
CompleteFile.WriteBuffer(FileName[1],Length(FileName));
except
end;


改成


try
FileName:=FilePath+ExtractFileName(FileList);
CopyFile(PChar(FileList),PChar(FileName),False);
FileName:=ExtractFileName(FileList)+#13#10;
CompleteFile.WriteBuffer(FileName[1],Length(FileName));
except
;
end;
 
iamy,你那么写不行啊
真的不能屏蔽吗?
 
iamy,改跟没改一样;try结构确实无法屏蔽,我也遇到过类似的问题;先判断FileList是否存在。
 
先测试以下才行:
Procedure DiskCanWrite:Boolean;
Var
Fl:File;
begin
assignfile(Fl,Path+'/a_a.d2d')
// 必须写一个几乎不用的文件名字!
{$I-}
rewrite(fl,1);
result:=(ioresult=0);
if result then
begin
closefile(fl);
erase(fl);
end;
end;
 
先创建文件,如果创建成功了则可写
然后再写
 

Similar threads

I
回复
0
查看
538
import
I
I
回复
0
查看
731
import
I
I
回复
0
查看
694
import
I
I
回复
0
查看
689
import
I
后退
顶部