请教前辈们一个问题
请教前辈一个问题:怎么实现一个Windows应用程序,有两个窗口,窗口放两个控件,ListView ,treeviewtreeview控件显示本地磁盘D盘各级文件夹作为treeview的节点,当用户选中treeview某个节点时,ListView控件显示节点所代表的文件夹的文件? --------------------编程问答-------------------- 先循环遍历D盘
将文件夹名存至一字符数组中
循环完毕之后 绑定至treeview 选择节点的点击事件
在中间加代码就OK了。 --------------------编程问答--------------------
private void Form1_Load(object sender, EventArgs e)
{
treeView1.Nodes.Add("D:\\");
treeView1.Nodes[0].Tag = "D:\\";
DirectoryInfo di = new DirectoryInfo("D:\\");
DirectoryInfo[] dirs = di.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
TreeNode tn = new TreeNode(dir.Name);
tn.Tag = dir.FullName;
treeView1.Nodes[0].Nodes.Add(tn);
AddChildNodes(tn);
}
}
private void AddChildNodes(TreeNode parent)
{
DirectoryInfo di = new DirectoryInfo(parent.Tag.ToString());
if (di.GetDirectories().Length > 0)
{
DirectoryInfo[] dirs = di.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
TreeNode tn = new TreeNode(dir.Name);
tn.Tag = dir.FullName;
parent.Nodes.Add(tn);
AddChildNodes(tn);
}
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
listView1.Items.Clear();
DirectoryInfo di = new DirectoryInfo(e.Node.Tag.ToString());
if (di.GetFiles().Length > 0)
{
FileInfo[] files = di.GetFiles();
foreach (FileInfo fi in files)
{
listView1.Items.Add(new ListViewItem(fi.Name));
}
}
}
补充:.NET技术 , C#