2013-11-22 223 views
2

我创建了一个程序,其中包含用户输入他的信息的登录系统,程序检查程序连接到的数据库以查看结果是否匹配,然后将用户记入日志。我想为每次用户登录时创建一个日志文件。日志文件的名称应该包含用户的用户名以及用户登录的日期和时间。我使用以下代码检查用户的凭据并写入他的细节到一个日志文件。此外,我想在文件名中的日期是像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; 

现在我的问题:我怎样才能创建文本文件与程序所在的当前目录不同的目录中?我在与程序本身相同的文件夹中有一个名为“日志”的文件夹。我希望日志在创建时保存到该文件夹​​中。

有什么建议吗?

+0

@JerryDodge,** **相对路径。顺便说一下,如果CWD设置正确,相对也会起作用。 –

+2

我建议你重新考虑将日志写入应用程序文件夹的子目录。这是可能导致程序不必要求提升权限的事情之一。建议的替代方法是使用Shell API获取适当的“用户文档”或“应用程序数据”文件夹。 –

+0

@克雷格也许这是一个便携式应用程序,意味着驻留在记忆棒上,这需要它的设置 –

回答

11

首先检索应用程序的路径,附加\Log\和文件名,然后提供AssignFile的完整路径。当然,如果您的应用程序安装在Windows %ProgramFiles%文件夹中,则它通常不具有对安装文件夹的写入访问权限(以下所有内容都假定您实际上具有对应用程序目录的写入访问权限。 )

var 
    LogFile: string; 
begin 
    // ... other code 
    LogFile := ExtractFilePath(Application.ExeName); 
    LogFile := IncludeTrailingPathDelimiter(LogFile) + 'Logs'; 
    LogFile := IncludeTrailingPathDelimiter(LogFile); 
    LogFile := LogFile + 
      'Log ' + 
      sUserName + 
      ' (' + sLoginDate 
      + ') ' + sLoginTime + '.txt'; 
    AssignFile(UserLogFile, LogFile); 
    // Write to log and other code 
end; 

如果您使用德尔福的更现代的版本,你可以使用的功能在TPath,使这个更容易一些:

uses 
    IOUtils;   // For TPath 

var 
    LogFile: string; 
begin 
    LogFile := TPath.Combine(ExtractFilePath(ParamStr(0)), 'Log'); 
    LogFile := TPath.Combine(LogFile, 
          'Log ' + 
          sUserName + 
         ' (' + sLoginDate 
         + ') ' + sLoginTime + '.txt'; 
    AssignFile(UserLogFile, LogFile); 
end; 

对于文件名部分,我更喜欢使用Format,而不是所有的级联的:

const 
    LogFileTemplate = 'Log %s(%s)%s.txt'; 

... 
var 
    LogFile: string; 
begin 
    LogFile := TPath.Combine(ExtractFilePath(ParamStr(0)), 'Log'); 
    LogFile := TPath.Combine(LogFile, 
          Format(LogFileTemplate, 
            [sUserName, sLoginDate, sLoginTime])); 
    AssignFile(UserLogFile, LogFile); 
    // Other code 
end; 

为了解决您的文件命名问题(日期/时间部分),你要它完全错误的。 :-)你做得太多了:

var 
    sDate: string; 

    sDate := FormatDateTime('(dd mmmm yyyy) hhmm', Now); 
    // Run now on my system shows (22 November 2013) 1645 
    ShowMessage(sDate); 

这意味着你的文件名被简化了。与单一sTimeStamp: string;更换sLoginDatesLoginTime,并使用这样的:

sTimeStamp := FormatDateTime('(dd mmmm yyyy) hhmm', Now); 
LogFile := LogFile + 
      'Log ' + 
      sUserName + 
      sTimeStamp + 
      '.txt'; 
+3

我建议宁愿使用ISO日期格式(yyyy-mm-dd),因为如果文件夹中有大量日志文件,这将按名称自然排序。 –

+0

@Glen号工作目录是工作目录。XP和Win7一样,实际上也是在Windows的所有现存版本上。 –

+0

@DavidHeffernan我说过工作目录吗?我说GetCurDir() –

相关问题