String ExePath;//路徑
ExePath=ExtractFilePath(Application->ExeName);//當前目錄
ExePath=ExtractFilePath(ExcludeTrailingPathDelimiter(GetCurrentDir()));//EXE程序的上一級目錄地址
WriteLog();//使用
.h
void __fastcall WriteLog(String AMsg);
.cpp
//-----------------在可執行程序目錄下寫入日志文件-------------------------------
void __fastcall WriteLog(String AMsg) {
static String LogFileName = ExePath+"log\\WebUpdata.log";
static String LogFile = ExePath+"log";
if (!DirectoryExists(LogFile))
{
CreateDir(LogFile);//文件夾不存在則創建
}
TFileStream *Log;
try {
if (!FileExists(LogFileName)) {
Log = new TFileStream(LogFileName, fmCreate);
}
else {
Log = new TFileStream(LogFileName, fmOpenWrite);
}
}
catch (...) {
return;
}
Log->Seek(0, soFromEnd);
AMsg = AMsg+"---->"+Now()+"\r\n";
Log->Write(AMsg.c_str(), AMsg.Length());
delete Log;
}
摘自 破空的專欄