delphi TTreeView組件遍曆磁碟目錄

來源:互聯網
上載者:User

標籤:style   http   color   使用   strong   io   檔案   資料   

TTreeView組件遍曆磁碟目錄 執行個體說明

TTreeView組件是一個以分枝結構或者說樹狀結構顯示資料的組件,以該組件顯示資料具有較好的等級關係和邏輯層次,並且易於操作。在組件中顯示的資料結構與系統中目錄的結構非常相似,所以本例使用該組件設計一個磁碟目錄查看工具。運行本例,在視窗右邊選擇目錄或路徑就可以在TTreeView組件中列出所有選擇目錄中的子目錄和檔案。執行個體運行效果1所示。

圖1  TTreeView組件遍曆磁碟目錄

 思路與技術

磁碟中的目錄結構就像TTreeView組件中顯示的一樣,具有較為明顯的階層,首先將一個目錄中的所有子目錄及檔案名稱添加到TTreeView組件中,然後再進入該目錄中的子目錄進行同上一步一樣的操作,持續進行多次直到進入最後一層停止,接著回到上一層對下一個目錄進行同樣的操作。這個過程可以用遞迴的方法完成。使用FindFirst函數可以列出目錄中所有的檔案及子目錄,該函數原型如下:

function FindFirst(const Path: string; Attr: Integer; var  F: TSearchRec): Integer;

參數說明:

l          Path:尋找的檔案,如果設定為*.*表示所有的檔案及目錄。

l          Attr: 檔案屬性。

l          F:搜尋資訊。

  注意:TTreeView組件的等級關係。

 開發步驟

(1)建立一個標準工程,建立一個新表單,預設主表單的Name屬性為Form1。

(2)在主表單中添加一個TLabel、TtreeView、TDriveComboBox、TdirectoryListBox 、TSaveDialog和TButton組件。

(3)設定TDirectoryListBox組件的Name屬性為DLB1,設定TDriveComboBox組件的Dirlist屬性為DLB1。

(4)程式完整代碼如下:

 

感覺有問題   //遍曆目錄下的所有檔案
var
    Directotynote, FileNode: TTreeNode;
procedure TForm1.myfind(bNode: TTreeNode;TreeView1:TTreeView);
var
  sr: TsearchRec;
  Err: Integer;
begin
   try
    Err := FindFirst(‘*.*‘, $37, sr);
     while (ERR = 0)  do
     begin
       if sr.Name[1] <> ‘.‘  then
       begin
         if (sr.Attr  and faDirectory) = 0  then
         begin
          TreeView1.Items.AddChildFirst(bNode, sr.Name);
         end;
         if (sr.Attr  and fadirectory) = faDirectory  then
         begin
          FileNode := TreeView1.Items.AddChildFirst(bnode, sr.Name);
          chdir(sr.Name);
          Application.ProcessMessages;
          myFind(FileNode,TreeView1);
          // chdir(‘..‘);
         end;
       end;
      err := FindNext(sr);
     end;
   except
   end;
end;



procedure TForm1.DLB1Change(Sender: TObject);
begin
  label2.Caption := dlb1.Directory;
  treeview1.Items.Clear;
   myFind(directotynote,TreeView1);
end;

 

 

 



來自為知筆記(Wiz)

附件列表

     

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.