本文整理自网络,侵删。
{新建一个TXT文档}Procedure NewTxt(FileName:String);Var F : Textfile; {定义 F 为 Textfile}Begin AssignFile(F,FileName); {将文件名与变量 F 关联} ReWrite(F); {创建Txt文档并命名为 “FileName ” } Closefile(F); {关闭文件 F}End;
{先附上原内容在写入新内容}Procedure AppendTxt(Str:String;FileName:String);Var F:Textfile;Begin AssignFile(F, FileName); Append(F); {附上原来的内容以免原内容被清空} Writeln(F, Str); {把内容 Ser 写入文件F } Closefile(F);End;
// 记录日志procedure WriteTxtLog(const strFileName, strContent: string);var strDir: string;begin try if (Trim(strFileName) = '') then exit; strDir := ExtractFileDir(strFileName); // 如果文件夹不存在,则创建 if (not DirectoryExists(strDir)) then begin ForceDirectories(strDir); Application.ProcessMessages; Sleep(100); Application.ProcessMessages; end; // 如果文件不存在,则创建 if (not FileExists(strFileName)) then begin NewTxt(strFileName); Application.ProcessMessages; Sleep(100); Application.ProcessMessages; end;
AppendTxt(strContent, strFileName); except ; end;end;
// 打印日志procedure WriteLog(const strText : string);var dt: TDateTime; strLogText, strLogFileName: string;begin dt := Now(); strLogText := FormatDateTime('yyyy-mm-dd hh:mm:ss', dt); strLogText := strLogText + ' ' + strText; frmMain.mmoLog.Lines.Add(strLogText); strLogFileName := FormatDateTime('yyyymmdd', dt); strLogFileName := g_strPath + 'log\' + strLogFileName + '.txt'; WriteTxtLog(strLogFileName, strLogText);end;
相关阅读 >>
winapi 字符及字符串函数(15): charnext、charprev
Delphi xe 7 mediaplayer 在安卓里放不出声音
更多相关阅读请进入《Delphi》频道 >>