报错的原因:
你可理解错了Rewrite()真正的用法,它会创建不存在的文件但不包括文件夹
当你想试图用这个方法创建一个新文件夹下的文件时就会报这个错误
解决办法:先判断文件夹是否存在,不存在先创建文件夹,然后用这个方法创建文件这样就可以了
var
FlogfilePath: string;
Fmylog: TextFile;
FsysDir: string;
Flogfile: string;
begin
//创建日志文件
FsysDir := extractfilepath(application.ExeName) + 'log\';
if not directoryexists(FsysDir) then
begin
createdir(FsysDir);
end;
FlogfilePath := extractfilepath(Paramstr(0)) + 'log\' + formatdatetime('yyyyy-mm-dd', Now()) + '.log';
AssignFile(Fmylog, FlogfilePath);
if fileexists(FlogfilePath) then
append(Fmylog)
else
rewrite(Fmylog);
Writeln(Fmylog, ALogText);
CloseFile(Fmylog);
end;
更多交流请加QQ:2609715119,请标注实在csdn上看到的!!!