我创建了一个程序,其中包含用户输入他的信息的登录系统,程序检查程序连接到的数据库以查看结果是否匹配,然后将用户记入日志。我想为每次用户登录时创建一个日志文件。日志文件的名称应该包含用户的用户名以及用户登录的日期和时间。我使用以下代码检查用户的凭据并写入他的细节到一个日志文件。此外,我想在文件名中的日期是像2013 1月23日。所以为的编码是之前的“与dmPredictGame做......”如何将文本文件保存到特定文件夹
sDate := DateToStr(Date());
sTime := TimeToStr(Time());
iYear := StrToInt(Copy(sDate,1,4));
iDay := StrToInt(Copy(sDate,9,2));
K := StrToInt(Copy(sDate,6,2));
Case K of
1 : sMonth := 'January';
2 : sMonth := 'February';
3 : sMonth := 'March';
4 : sMonth := 'April';
5 : sMonth := 'May';
6 : sMonth := 'June';
7 : sMonth := 'July'; //Check for the current month
8 : sMonth := 'August';
9 : sMonth := 'September';
10 : sMonth := 'Oktober';
11 : sMonth := 'November';
12 : sMonth := 'December';
end;
sTime1 := copy(sTime,1,2);
sTime2 := copy(sTime,4,2);
sLoginTime := sTime1 + ';' + sTime2; //Use ; because windows does not allow : in file names
sLoginDate := IntToStr(iDay) + ' ' + sMonth + ' ' + IntToStr(iYear);
with dmPredictGame do
begin
if tblUserInfo.Locate('Username', edtUsername.Text, []) AND ((edtPassword.Text) = tblUserInfo['Password']) then //Check if the username and password is correct.
begin
MessageDlg('Login was successful! Welcome back ' + edtUsername.Text, mtInformation, [mbOK],0);
edtUsername.Text := tblUserInfo['Username'];
begin
sUsername := edtUsername.Text;
sPassword := tblUserInfo['Password'];
sName := tblUserInfo['Name'];
sSurname := tblUserInfo['Surname'];
assignFile(UserLogFile, 'Log ' + sUsername + ' (' + sLoginDate + ') ' + sLoginTime + '.txt');
Rewrite(UserLogFile);
writeln(UserLogFile, 'Username: ' + sUsername);
writeln(UserLogFile, 'Password: ' + sPassword);
writeln(UserLogFile, 'Name: ' + sName);
writeln(UserLogFile, 'Surname: ' + sSurname);
writeln(UserLogFile, 'Date Logged In: ' + sDate);
writeln(UserLogFile, 'Time Logged In: ' + sTime);
closeFile(UserLogFile);
end;
现在我的问题:我怎样才能创建文本文件与程序所在的当前目录不同的目录中?我在与程序本身相同的文件夹中有一个名为“日志”的文件夹。我希望日志在创建时保存到该文件夹中。
有什么建议吗?
@JerryDodge,** **相对路径。顺便说一下,如果CWD设置正确,相对也会起作用。 –
我建议你重新考虑将日志写入应用程序文件夹的子目录。这是可能导致程序不必要求提升权限的事情之一。建议的替代方法是使用Shell API获取适当的“用户文档”或“应用程序数据”文件夹。 –
@克雷格也许这是一个便携式应用程序,意味着驻留在记忆棒上,这需要它的设置 –