根據昨天的程式,稍作改良,就可以把整個目錄樹丟進去顯示了…
下面就是利用 Recursive 來把資料塞到 TreeStore 裡面去。
public void _buildTreeStore( TreeStore store, DirectoryInfo di, TreeIter parent ) { foreach( DirectoryInfo iter in di.GetDirectories() ) { TreeIter ti = store.AppendValues( parent, iter.FullName, iter.Name, "" ); _buildTreeStore( store, iter, ti, true ); } // add files in tree foreach( FileInfo fi in di.GetFiles() ) store.AppendValues( parent, fi.FullName, fi.Name, fi.Length.ToString() ); } public TreeStore buildTreeStore( string path ) { // 0: fullpath 1: name 2: filesize TreeStore store = new TreeStore( typeof(string), typeof(string), typeof(string) ); DirectoryInfo di = new DirectoryInfo( path ); TreeIter root = store.AppendValues( path, di.Name, "" ); _buildTreeStore( store, di, root ); return store; } public ExplorerSharp(): base(Gtk.WindowType.Toplevel) { Build(); TreeStore store = buildTreeStore( "./" ); // 設定資料來源 treeview1.Model = store; // 不顯示表頭 treeview1.HeadersVisible = false; // 設定要顯示的欄位 treeview1.AppendColumn("Name", new CellRendererText(), "Name", 1 ); treeview1.AppendColumn("Size", new CellRendererText(), "FileSize", 2 ); // 一次只能選定一列或一個節點 treeview1.Selection.Mode = Gtk.SelectionMode.Single; }