复制时出错
目的是想实现把一文件复制到U盘里去,目标U盘是通过ComboBox选择;
但编译是...message "I/O error 21 "....
请问该如何改正 ?
代码如下...
procedure TForm1.Mycopyfile(a,b: string);
var
from,aa: file of byte;
buffer: array[0..4069]of char;
numread: integer;
filelength: longint;
begin
try
AssignFile(from,a);
Reset(from);
AssignFile(aa,b);
ReWrite(aa);
FileLength := filesize(from); //文件大小
except
Showmessage( '文件复制错误! ');
exit;
end;
with progressbar1 do
begin
min := 0;
position:=min;
max := filelength;
while filelength > 0 do
begin
BlockRead(from,buffer[0],sizeof(buffer),numread);
filelength := filelength - numread;
BlockWrite(aa,buffer[0],numread);
position:= position + numread;
application.ProcessMessages;
end;
CloseFile(from);
CloseFile(aa);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i,DType:integer;
Disk:string;
begin
ProgressBar1.Hide;
for i:=65 to 90 do
begin
Disk:=chr(i)+ ':\ ';
DType := GetDriveType(PChar(Disk));
if DType = 5 then
ComboBox1.Items.Add(Format( '%s %s ',[Disk, '可移动磁盘 ']));
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ProgressBar1.Show;
mycopyfile( 'C:\bmp.bmp ',Copy(ComboBox1.Text,1,3)+ 'bmp.bmp ');
//mycopyfile( 'C:\bmp.bmp ', 'J:\bmp.bmp ')); 这样可以
Application.MessageBox( '复制完毕! ', '提示 ',MB_ICONASTERISK);
ProgressBar1.Hide ;
Edit3.Text:=Copy(ComboBox1.Text,1,3)+ 'bmp.bmp ';
end;
[解决办法]
把 if DType = 5 then 这句,改为 if DType = DRIVE_REMOVABLE then......
关于GetDriveType函数的返回值,请查看Delphi的WindowsSDK帮助
[解决办法]
你可以改一下Mycopyfile(a,b: string)函数,参数为数组(或记录),要拷贝的每一个文件为一个数组元素。然后,在 with progressbar1 do 这里面做一个循环,完成每一个元素(每一个文件)的拷贝。
具体实现,你自己写代码吧。