标签:nta vat directory private clear namespace tno forms file
TreeView、ListView显示电脑的文件夹及文件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace TraversMagneticDiskCatalogue { public partial class Form1 : Form { TreeNode selectNode; public Form1() { InitializeComponent(); lstViewInfo.View = View.List; this.Load += Form1_Load; trViewInfo.AfterSelect += TrViewInfo_AfterSelect; trViewInfo.NodeMouseDoubleClick += TrViewInfo_NodeMouseDoubleClick; contextMenuStrip1.Click += ContextMenuStrip1_Click; } private void ContextMenuStrip1_Click(object sender, EventArgs e) { string path = selectNode+ lstViewInfo.SelectedItems[0].Text; System.Diagnostics.Process.Start(path); } private void Form1_Load(object sender, EventArgs e) { lstViewInfo.ContextMenuStrip = contextMenuStrip1; trViewInfo.ImageList = imageList1; TreeNode rootNode = trViewInfo.Nodes.Add("我的电脑", "我的电脑", 1, 0); //TreeViewShow(rootNode,rootNode.Tag.ToString()); } private void TrViewInfo_AfterSelect(object sender, TreeViewEventArgs e) { selectNode = new TreeNode((string)e.Node.Tag); TreeViewShow(e.Node,(string)e.Node.Tag); ListViewShow(e.Node); } private void TreeViewShow(TreeNode subNode,string path) { try { if (subNode.Nodes.Count == 0) { if (subNode.Parent == null) { foreach (string driverName in Directory.GetLogicalDrives()) //获取电脑逻辑盘 { TreeNode driverNode = new TreeNode(); driverNode.Tag = driverName; driverNode.Text = driverName.Substring(0, 1); driverNode.SelectedImageIndex = 2; driverNode.ImageIndex = 3; subNode.Nodes.Add(driverNode); } } else { DirectoryInfo directoryInfo = new DirectoryInfo(path); int countDir = Directory.GetDirectories(path).Length; foreach (DirectoryInfo dirName in directoryInfo.GetDirectories()) //获取文件夹 { TreeNode dirNode = new TreeNode(); dirNode.Tag = dirName.FullName; dirNode.Text = dirName.Name; dirNode.ImageIndex = 4; dirNode.SelectedImageIndex =4; subNode.Nodes.Add(dirNode); } DirectoryInfo dirinfo = new DirectoryInfo(path); //int countfiles = Directory.GetFiles(path).Length; foreach (FileInfo fileName in dirinfo.GetFiles("*.*")) //获取文件 { TreeNode dirNode = new TreeNode(); dirNode.Tag = fileName.FullName; dirNode.Text = fileName.Name; dirNode.ImageIndex = 6; dirNode.SelectedImageIndex = 6; subNode.Nodes.Add(dirNode); } } } } catch { } } private void ListViewShow(TreeNode treeNode) { lstViewInfo.Items.Clear(); try { if (treeNode.Parent == null) { foreach (DriveInfo driver in DriveInfo.GetDrives()) { ListViewItem viewItem = new ListViewItem(driver.Name.Substring(0, 1)); lstViewInfo.Items.Add(viewItem); } } else { DirectoryInfo directory = new DirectoryInfo((string)treeNode.Tag); foreach (DirectoryInfo dir in directory.GetDirectories()) { ListViewItem viewItem = new ListViewItem(dir.Name); lstViewInfo.Items.Add(viewItem); } foreach (FileInfo file in directory.GetFiles()) { ListViewItem viewItem = new ListViewItem(file.Name); lstViewInfo.Items.Add(viewItem); } } } catch { } } private void TrViewInfo_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) { DirectoryInfo directory = new DirectoryInfo((string)e.Node.Tag); FileInfo fileInfo = new FileInfo((string)e.Node.Tag); if (directory.Exists) { folderBrowserDialog1.SelectedPath = (string)e.Node.Tag; folderBrowserDialog1.ShowDialog(); } if(fileInfo.Exists) { System.Diagnostics.Process.Start((string)e.Node.Tag); } } } }
效果
标签:nta vat directory private clear namespace tno forms file
原文地址:https://www.cnblogs.com/wenjie0904/p/9678280.html