procedure TForm1.N1Click(Sender: TObject);
var
  I: Integer;  procedure GetFileDir(S: string);
  var
    Found: Integer;
    SearchRec: TSearchRec;
  begin
    Found := FindFirst(S + '\*.*', faAnyFile, SearchRec);
    while Found = 0 do
    begin
      ListView1.Items.Add;
      ListView1.Items[I].Caption := S + '\' + SearchRec.Name;
      Inc(I);
      if FileGetAttr(S + '\' + SearchRec.Name) = faDirectory then
        GetFileDir(S + '\' + SearchRec.Name);
      Found := FindNext(SearchRec);
    end;
    FindClose(SearchRec);
  end;begin
  I := 0;
  GetfileDir('C:');
end;我想把C盘里所以的目录和文件全部都加入在listview控件里面...可是为什么以上代码不行..  还有一个问题就是listview里面可以加入多少条..有没有限制...

解决方案 »

  1.   

    1. FindFirst, FindNext, FindClose有不止一个版本,看你的写法应指明为sysutils.Find....
    2. 兄弟,为什么用I做临时变量,而不是定义一个TListItem对象?
    3. SearchRec.Attr被浪费了,用FileGetAttr还不如用DirectoryExistsListView的条目在理论上没有什么限制,看你的机器了
      

  2.   

    sysutils.Find....  你的这个不是很明白?第2个和第3个我会改的...另外你还没有帮我把整个C盘的目录和文件递归出来?  谢谢!
      

  3.   

    下面这些代码来自Lysee 1.0.8中的LiShell.Pas,下载地址:
    http://lysee.oicp.net/downloads/lysee108_2232.exe
    ======================================================
    function copyTree(const sourceDir, destiDir: string): boolean;
    var
      S, D: string;
    begin
      S := ExpandFileName(Trim(sourceDir));
      Result := DirectoryExists(S);
      if not Result then Exit;  S := IncludeTrailingPathDelimiter(S);
      D := includeTrailingPathDelimiter(ExpandFileName(Trim(destiDir)));
      if sameFileName(S, D) then Exit;  Result := copyFiles(S + '*.*', D, true);
    end;function copyFiles(const fmask, destiDir: string; copyDir: boolean): boolean;
    var
      S, P, Path: string;
      R: TSearchRec;
    begin
      Result := false;  Path := ExpandFileName(Trim(destiDir));
      if not DirectoryExists(Path) and not ForceDirectories(Path) then Exit;
      Path := IncludeTrailingPathDelimiter(Path);  S := ExpandFileName(Trim(fmask));
      if SysUtils.FindFirst(S, faAnyFile, R) <> 0 then Exit;
      try
        P := ExtractFilePath(S);
        Result := true;
        repeat
          if (R.Name <> '.') and (R.Name <> '..') then
          begin
            S := P + R.Name;
            if (R.Attr and faDirectory) = 0 then
              Result := CopyFile(PChar(S), PChar(Path + R.Name), false) else
            if copyDir then
            begin
              S := IncludeTrailingPathDelimiter(S);
              Result := copyFiles(S + '*.*', Path + R.Name, copyDir);
            end;
          end;
        until not Result or (SysUtils.FindNext(R) <> 0);
      finally
        SysUtils.FindClose(R);
      end;
    end;function removeTree(const dir: string): boolean;
    var
      P: string;  function rm_dir(const path: string): boolean;
      var
        R: TSearchRec;
      begin
        Result := true;
        if SysUtils.FindFirst(path + '*.*', faAnyFile, R) = 0 then
        try
          repeat
            if (R.Name <> '.') and (R.Name <> '..') then
              if (R.Attr and faDirectory) = 0 then
                Result := SysUtils.DeleteFile(path + R.Name) else
                Result := rm_dir(IncludeTrailingPathDelimiter(path + R.Name));
          until not Result or (SysUtils.FindNext(R) <> 0);
        finally
          SysUtils.FindClose(R);
        end;
        if Result then
          Result := SysUtils.RemoveDir(ExcludeTrailingPathDelimiter(path));
      end;  function do_rmv_dir: boolean;
      begin
        Result := not DirectoryExists(P);
        if not Result then
          Result := rm_dir(IncludeTrailingPathDelimiter(P));
      end;begin
      P := ExpandFileName(Trim(dir));
      Result := do_rmv_dir or do_rmv_dir;
    end;