Delphi 创建目录及写日志文件、写INI文件
2010-12-07 23:30
351 查看
/_AppPath
全局变量
_AppPath :=
ExtractFilePath(ParamStr(0));
1、定义类型
var
F: TextFile;
LogFile: string;
txt :string;
2、代码
//创建目录
if not directoryexists(_AppPath + 'Error/')
then
createdir(_AppPath + 'Error/');
LogFile := _AppPath +
'Error/' + FormatDateTim('YYYYMMDD',Date) +
'.txt';
//写日志文件
AssignFile(F, LogFile);
if FileExists(LogFile) then
Append(F)
else
Rewrite(F);
//开始写入
Writeln(F, txt);
//结束
CloseFile(F);
另外附加一段写INI文件的代码:
with
TIniFile.Create(_AppPath + 'F7.INI') do
begin
//读
AutoTime :=
ReadInteger('System','AutoTime', 900000);
nCount := ReadInteger('TIMELIST1', 'COUNT', 0);
if nCount
> 0 then
begin
TimeList1.Lines.Clear;
for i := 0 to nCount - 1 do
TimeList1.Lines.Add(ReadString('TIMELIST1', IntToStr(i),''));
end;
Free;
end;
//写
with
TIniFile.Create(_AppPath + 'F7.INI') do
begin
for i := 0
to TimeList1.Lines.Count -1 do
begin
s := Trim(TimeList1.Lines.Strings[i]);
if s <> '' then
begin
Inc(j);
WriteString('TIMELIST1', IntToStr(i), s);
end;
end;
WriteInteger('TIMELIST1', 'COUNT', j);
Free;
end;
最佳答案
全局变量
_AppPath :=
ExtractFilePath(ParamStr(0));
1、定义类型
var
F: TextFile;
LogFile: string;
txt :string;
2、代码
//创建目录
if not directoryexists(_AppPath + 'Error/')
then
createdir(_AppPath + 'Error/');
LogFile := _AppPath +
'Error/' + FormatDateTim('YYYYMMDD',Date) +
'.txt';
//写日志文件
AssignFile(F, LogFile);
if FileExists(LogFile) then
Append(F)
else
Rewrite(F);
//开始写入
Writeln(F, txt);
//结束
CloseFile(F);
另外附加一段写INI文件的代码:
with
TIniFile.Create(_AppPath + 'F7.INI') do
begin
//读
AutoTime :=
ReadInteger('System','AutoTime', 900000);
nCount := ReadInteger('TIMELIST1', 'COUNT', 0);
if nCount
> 0 then
begin
TimeList1.Lines.Clear;
for i := 0 to nCount - 1 do
TimeList1.Lines.Add(ReadString('TIMELIST1', IntToStr(i),''));
end;
Free;
end;
//写
with
TIniFile.Create(_AppPath + 'F7.INI') do
begin
for i := 0
to TimeList1.Lines.Count -1 do
begin
s := Trim(TimeList1.Lines.Strings[i]);
if s <> '' then
begin
Inc(j);
WriteString('TIMELIST1', IntToStr(i), s);
end;
end;
WriteInteger('TIMELIST1', 'COUNT', j);
Free;
end;
我写了一个函数用来写日志,但是写的时候提示出错。I/O error 32。我查了下错误原因应该是定时器同时写入的问题,然后我用了2中线程方法处理,但是都没用,结果一样,帮忙看下怎么回事。 procedure TEventLog.RecordLog(Log: string); var f:Textfile; FileName:string; name:string; begin if WaitForSingleObject(hMutex, INFINITE) = WAIT_OBJECT_0 then begin name:=FormatDateTime('yyyymmdd',now); FileName:=ExtractFilePath(Paramstr(0))+'log/'+name+'.log'; //保存文件名 AssignFile(f, FileName); try if FileExists(FileName)= False then Rewrite(f) else Append(f); Writeln(f,FormatDateTime('yy''-''mm''-''dd hh:nn:ss ',Now) + Log); finally CloseFile(f); end; ReleaseMutex(hMutex); end; end; procedure TEventLog.RecordLog(Log: string); var f:Textfile; FileName:string; name:string; begin criticalsection.enter; try name:=FormatDateTime('yyyymmdd',now); FileName:=ExtractFilePath(Paramstr(0))+'log/'+name+'.log'; //保存文件名 AssignFile(f, FileName); try if FileExists(FileName)= False then Rewrite(f) else Append(f); Writeln(f,FormatDateTime('yy''-''mm''-''dd hh:nn:ss ',Now) + Log); finally CloseFile(f); end; finally criticalsection.leave; end; end;
最佳答案
出错原因:因为访问文件的方式是独占式,两个地方同时访问了这个文件。 将 Textfile 设置为全局变量,程序初始化时,载入Textfile,将所有写Textfile 文件的操作,都由一个函数来执行,程序关闭时才CloseFile(Textfile) 。 追加文件内容速度很快,但是载入文件和关闭文件是非常缓慢的,这两过程所消耗的时间非常巨大的。一直载入/关闭的话,一点效率也没有。小文件还行,只要上M就可以明显感觉出来了。
相关文章推荐
- Delphi 创建目录及写日志文件
- Delphi 创建目录及写日志文件
- MySQL 5.7.19安装目录下创建my.ini文件的方法
- python-按日志提取文件并创建相关目录
- Java API 自带的日志管理,可配置文件路径,并自动创建不存在的目录
- 封装Ftp API函数,实现上传,下载文件,创建目录(Delphi)
- MySQL 5.7.20安装目录下自定义创建my.ini文件
- delphi中INI 文件的创建与使用
- [delphi]动态创建MEMO历遍目录TXT文件到listbox中,並將內容加載到memo中.
- Java API 自带的日志管理,可配置文件路径,并自动创建不存在的目录
- delphi中INI 文件的创建与使用
- FTP文件操作之创建目录
- Linux在目录中进入目录创建文件需要哪些权限
- 用Delphi创建IIS虚拟目录
- 《Windows API》创建文件写入系统目录路径
- Delphi CreateProcess WIN32API函数CreateProcess用来创建一个新的进程和它的主线程,这个新进程运行指定的可执行文件
- Delphi 把客户端的文件或者目录上传到服务器端
- 《PHP---自定义创建目录文件方法》----如果目录已存在,该目录必须为空才可继续创建
- java创建文件和目录
- VC中判断目录,文件是否存在,创建目录的方法