NEWS

Duyệt tập tin và thư mục giống Explorer với TreeView và ListView

Duyệt  tập tin và thư mục giống Explorer với TreeView và ListView
Đăng bởi: Mr. Cùi Bắp - Lượt xem: 24814 00:39:19, 18/10/2015C#   In bài viết

Trong bài này mình chia sẻ với các bạn cách duyệt thư mục và tập tin trong máy tinh giống như Windows Explorer sử dụng 2 control là TreeView & ListView C#

[C#] Hướng dẫn sử dụng Treeview và ListView 

Đầu tiên các bạn thiết kế form giống như trong hình và sau đó code những đoạn bên dưới

Hàm này để thêm tập tin & thư mục vào TreeView

private void AddDirectories(TreeNode tnSubNode)
	{	
		treeView1.BeginUpdate();
		iDirectories = 0;
		try
		{			
			DirectoryInfo diRoot;
			// Nếu là ổ đĩa thì lấy thư mục từ ổ đỉa
			if(tnSubNode.SelectedImageIndex < 11)	
			{
				diRoot = new DirectoryInfo(tnSubNode.FullPath + "");								
			}
			//  Ngược lại lấy thư mục từ thư mục
			else	
			{
				diRoot = new DirectoryInfo(tnSubNode.FullPath);									
			}
			DirectoryInfo[] dirs = diRoot.GetDirectories();

			tnSubNode.Nodes.Clear();

			// Add thư mục con vào tree
			foreach(DirectoryInfo dir in dirs)
			{
				iDirectories++;
				TreeNode subNode = new TreeNode(dir.Name);
				subNode.ImageIndex = 11;
				subNode.SelectedImageIndex = 12;					
				tnSubNode.Nodes.Add(subNode);					
			}
		}
		catch	{	;	}			 		
		treeView1.EndUpdate();
	}

Còn hàm này để thêm tập tin vào ListView

private void AddFiles(string strPath)
	{		
		listView1.BeginUpdate();
		listView1.Items.Clear();
		iFiles = 0;
		try
		{
			DirectoryInfo di = new DirectoryInfo(strPath + "");			
			FileInfo[] theFiles = di.GetFiles();
			string _Size = string.Empty;
			foreach(FileInfo theFile in theFiles)
			{
				iFiles++;
				if (theFile.Length >= 1024)
					_Size = string.Format("{0:### ### ###} KB", theFile.Length / 1024);
				else _Size = string.Format("{0} Bytes", theFile.Length);

				ListViewItem lvItem = new ListViewItem(theFile.Name);
				lvItem.SubItems.Add(_Size);
				lvItem.SubItems.Add(theFile.LastWriteTime.ToString("dd/MM/yyyy"));
				lvItem.SubItems.Add(theFile.LastWriteTime.ToShortTimeString());
				listView1.Items.Add(lvItem);									
			}
		}
		catch(Exception Exc)	{	statusBar1.Text = Exc.ToString();	}
		listView1.EndUpdate();		
	}

Sự kiện khi Form_Load

private void Form1_Load(object sender, System.EventArgs e)
	{
		string[] aDrives = Environment.GetLogicalDrives();
		treeView1.BeginUpdate();
		foreach(string strDrive in aDrives)
		{		
			TreeNode dnMyDrives = new TreeNode(strDrive.Remove(2,1));
			
			switch (strDrive)
			{	
				case "A:":
					dnMyDrives.SelectedImageIndex = 0;
					dnMyDrives.ImageIndex = 0;
					break;
				case "C:":
					treeView1.SelectedNode = dnMyDrives;    
					dnMyDrives.SelectedImageIndex = 1;		
					dnMyDrives.ImageIndex = 1;
					
					break;
				case "D:":
					dnMyDrives.SelectedImageIndex = 2;
					dnMyDrives.ImageIndex = 2;
					break;				
				default:
					dnMyDrives.SelectedImageIndex = 3;
					dnMyDrives.ImageIndex = 3;
					break;
			}
			treeView1.Nodes.Add(dnMyDrives);			
		}		
		treeView1.EndUpdate();
	}

Sự kiện sau khi Tree được click

private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
	{		
		// Thêm thư mục con từ ổ đĩa vào tree
		AddDirectories(e.Node);		
		treeView1.SelectedNode.Expand();			
		// Thêm file vào listview
		AddFiles(e.Node.FullPath.ToString());
		statusBar1.Text = iDirectories.ToString() + " Thư mục(s)  " + iFiles.ToString() + " Tệp(s)";
	}

Sự kiện khi Click vào các Item trong ListView

private void listView1_ItemActivate(object sender, System.EventArgs e)
	{
		try
		{
			string sPath = treeView1.SelectedNode.FullPath;
			string sFileName = listView1.FocusedItem.Text;
			Process.Start(sPath + "" + sFileName);
		}
		catch(Exception Exc)	{	MessageBox.Show(Exc.ToString());	}    
	}

Chúc các bạn vui và thành công!

Download source

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

Duyệt  tập tin và thư mục giống Explorer với TreeView và ListView
Đăng bởi: Mr. Cùi Bắp - Lượt xem: 24814 00:39:19, 18/10/2015C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.