TListView元件顯示資料夾中的檔案 (轉)

worldblog發表於2008-01-31
TListView元件顯示資料夾中的檔案 (轉)[@more@]

procedure GetDirectories(list: TlistView; Directory: string; Item: Tlistitem; IncludeFiles: boolean);
var
SearchRec: TSearchRec;
begin
  list.Items.BeginUpdate;//準備
  list.Items.Clear;  //清空list內容
  if Directory[length(Directory)] <> '' then //判斷路徑 
  Directory := Directory + ''; //設定路徑
 if FindFirst(Directory + '*.*',faDirectory,SearchRec) = 0 then  begin
  repeat 
  if (SearchRec.Attr and faDirectory = faDirectory) and (SearchRec.Name[1] <> '.') then 
  begin
  if (SearchRec.Attr and faDirectory > 0) then  begin Item := list.Items.Add;  //增加item
  item.Caption:=SearchRec.Name; 
  item.ImageIndex:=6;
  end;
  GetDirectories(list,Directory + SearchRec.Name,Item,IncludeFiles);
 end
  else
 if IncludeFiles then
  if SearchRec.Name[1] <> '.' then
  begin
  item:=list.Items.Add;
  item.Caption:=SearchRec.Name;
  item.ImageIndex:=6;
  end;
 until FindNext(SearchRec) <> 0;
//FindClose(SearchRec);
  end;
  list.Items.EndUpdate;
end;

定義Directory:

procedure makedir(modulname,pathname,dirname,filepath:string);//建立目錄(共4級,朋友如果覺得用得上的話,可以自己加引數)
begin
try
  if not directoryexists(fpath+modulname) then
  createdir(FRootPath+modulname);
  if not DirectoryExists(FRootPath+modulname+''+pathname) then
  createdir(FRootPath+modulname+''+pathname);
  if not directoryexists(FRootPath+modulname+''+pathname+''+dirname) then
  createdir(FRootPath+modulname+''+pathname+''+dirname);
  if filepath<>'' then
  if not directoryexists(FRootPath+modulname+''+pathname+''+dirname+''+filepath) then
  createdir(FRootPath+modulname+''+pathname+''+dirname+''+filepath)
  except
  On E:Exception do begin
  abort;
  end;
  end;
  banjin.N21.Enabled:=true;
  liucheng.MenuItem1.Enabled:=true;
  yanzheng.N21.Enabled:=true;
  banjin.Speeutton1.Enabled:=true;
  liucheng.SpeedButton1.Enabled:=true;
  yanzheng.SpeedButton1.Enabled:=true;
  banjin.toolbutton1.Enabled:=true;
  liucheng.toolbutton1.Enabled:=true;
  yanzheng.toolbutton1.Enabled:=true;
  if filepath='' then
  filespath:=FRootPath+modulname+''+pathname+''+dirname+''+filepath else
  filespath:=FRootPath+modulname+''+pathname+''+dirname+''+filepath+'';
  banjin.Statar1.Panels[0].Text:='你目前所在的位置:'+filespath;
  liucheng.StatusBar1.Panels[0].Text:='你目前所在的位置:'+filespath;
  yanzheng.StatusBar1.Panels[0].Text:='你目前所在的位置:'+filespath;
end;
如果是二級或一級目錄的話,只要加上if filepath<>'' then
類似的判斷就可以了,例如:makedir('設計規範','電子件','電路設計規範','');

 

注:在這裡寫的兩個過程沒有實現讀取的圖示,如果想實現這一功能的話要自己動手寫了 ^_^


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-998895/,如需轉載,請註明出處,否則將追究法律責任。

相關文章