复制文件的简单问题
procedure TForm1.Button2Click(Sender: TObject);
var
dir,dir2:string;
begin
dir:= 'd: '+ '\ '+Pchar(label1.Caption);
if not directoryexists(dir) then
if not createdir(dir) then
begin
showmessage( '无法创建 ');
end;
dir2:=dir+ '\ '+ExtractFileName(openDialog1.FileName);
CopyFile(Pchar(edit1.text),Pchar(dir2),true);
end;
譬如label1.caption为test,我这样写的话,只是在d盘下创建了test文件夹,但文件并未复制到文件夹中。
如果我直接写dir:= 'd: '+ '\ '+ 'test '的话,就创建test文件夹并且把文件复制到test文件夹中。
请问如果我非要用label1.caption的话,要怎样写呢?
[解决办法]
dir:= 'd: '+ '\ '+ 'test ' 是一个字符串
dir:= 'd: '+ '\ '+Pchar(label1.Caption)是一个以#0结尾的串,差别就在这个#0
非要用label1.caption的话就不要PChar,dir:= 'd: '+ '\ '+label1.Caption;
[解决办法]
dir:= 'd: '+ '\ '+ 'test ' 为什么这样呢 'd:\test ' 不就行了, 都是字符型那么多+
dir:= 'd: '+ '\ '+Pchar(label1.Caption); 改 dir:=Pchar( 'd:\ '+label1.Caption);
CopyFile(edit1.text,dir2,true); 那么喜欢pchar啊, 都去掉