大家好,以下代码是从一个目录将相关的文件名读出来添加到combobox中,但是有些细节我不是很明白,特此请教一下。代码如下:
FindFirst(cPath + '*.*',faAnyFile,SearchRec);
if (SearchREc.Name <> '.') and (SearchREc.Name<>'..') then//问题一这个判断是什么作用,'.'和'..'分别代表什么
    ComboBox1.Items.Add(SearchRec.Name);                       
    
while FindNext(SearchREc)=0 do //问题二这是判断是否找到文件吗?找到了为什么还要进行以下判断?
begin
   if (SearchREc.Name <> '.') and (SearchREc.Name<>'..') then
     ComboBox1.Items.Add(SearchRec.Name);
end;
FindClose(SearchREc);
就是以上两个问题,敬请指点。在线恭候。

解决方案 »

  1.   

     FindNext(SearchREc)=0 表示找到文件,下面为delphi中源码function FindFirst(const Path: string; Attr: Integer;
      var  F: TSearchRec): Integer;
    const
      faSpecial = faHidden or faSysFile or faVolumeID or faDirectory;
    {$IFDEF MSWINDOWS}
    begin
      F.ExcludeAttr := not Attr and faSpecial;
      F.FindHandle := FindFirstFile(PChar(Path), F.FindData);
      if F.FindHandle <> INVALID_HANDLE_VALUE then
      begin
        Result := FindMatchingFile(F);
        if Result <> 0 then FindClose(F);
      end else
        Result := GetLastError;
    end;
    function FindMatchingFile(var F: TSearchRec): Integer;
    {$IFDEF MSWINDOWS}
    var
      LocalFileTime: TFileTime;
    begin
      with F do
      begin
        while FindData.dwFileAttributes and ExcludeAttr <> 0 do
          if not FindNextFile(FindHandle, FindData) then
          begin
            Result := GetLastError;
            Exit;
          end;
        FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
        FileTimeToDosDateTime(LocalFileTime, LongRec(Time).Hi,
          LongRec(Time).Lo);
        Size := FindData.nFileSizeLow;
        Attr := FindData.dwFileAttributes;
        Name := FindData.cFileName;
      end;
      Result := 0;
    end;