.net程序设计文件操作
作者:互联网
一、今日学习内容——.net
今天完成了实验四:文件操作
代码:
using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form4 : Form { public Form4() { InitializeComponent(); TreeNode myComputerNode = new TreeNode("我的电脑"); treeView1.Nodes.Add(myComputerNode); listViewShow(myComputerNode); } //编写 ListView 控件的方法 listViewShow(TreeNode dirNode) private void listViewShow(TreeNode dirNode) { listView1.Clear(); if (dirNode.Parent == null) { foreach (string DrvName in Directory.GetLogicalDrives()) { ListViewItem aItem = new ListViewItem(DrvName); listView1.Items.Add(aItem); } } else { foreach (string DirName in Directory.GetDirectories((string)dirNode.Tag)) { ListViewItem aItem = new ListViewItem(DirName); listView1.Items.Add(aItem); } foreach (string fileName in Directory.GetFiles((string)dirNode.Tag)) { ListViewItem aItem = new ListViewItem(fileName); listView1.Items.Add(aItem); } } } //listview的重载方法 private void listViewShow(string dirName) { listView1.Clear(); try { foreach (string DirName in Directory.GetDirectories(dirName)) { ListViewItem aItem = new ListViewItem(DirName); listView1.Items.Add(aItem); } foreach (string fileName in Directory.GetFiles(dirName)) { ListViewItem aItem = new ListViewItem(fileName); listView1.Items.Add(aItem); } } catch { } } //编写 DirTreeShow(TreeNode dirNode)方法 private void DirTreeShow(TreeNode dirNode) { try { if (dirNode.Nodes.Count == 0) { if (dirNode.Parent == null) { foreach (string DrvName in Directory.GetLogicalDrives()) { TreeNode aNode= new TreeNode(DrvName); aNode.Tag=DrvName; dirNode.Nodes.Add(aNode); } } else { foreach(string DirName in Directory.GetDirectories((string)dirNode.Tag)) { TreeNode aNode=new TreeNode(DirName); aNode.Tag=DirName; dirNode.Nodes.Add(aNode); } } } } catch { } } //编写 TreeView 控件的 AfterSelect 事件代码 private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { listViewShow(e.Node); DirTreeShow(e.Node); } //编写 ListView 控件的 DoubleClick 事件代码。 private void listView1_SelectedIndexChanged(object sender, EventArgs e) { foreach (int listIndex in listView1.SelectedIndices) { listViewShow(listView1.Items[listIndex].Text); } } } }
运行截图:
二、遇到的问题
缺少相应控件的事件代码,运行报错
三、明日计划
明天继续完成大作业
标签:listView1,TreeNode,string,文件,aItem,程序设计,net,ListViewItem,dirNode 来源: https://www.cnblogs.com/wmdww/p/15568074.html