namespace 资料管理
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo mulu = new DirectoryInfo("新建文件夹");
DirectoryInfo[] dd = mulu.GetDirectories();
foreach (DirectoryInfo item in dd)
{
TreeNode tnode = treeView1.Nodes.Add(item.Name);
loaddata(item.FullName, tnode);
}
}
//递归获取当前目录下所以的子文件夹
private void loaddata(string path, TreeNode tnode)
{
string[] file = Directory.GetFiles(path);
//把这些文件加到treeView里面
foreach (string item in file)
{
TreeNode nodefile = tnode.Nodes.Add(Path.GetFileName(item));
nodefile.Tag = item;
}
string[] dir = Directory.GetDirectories(path);
foreach (var item in dir)
{
TreeNode tree = tnode.Nodes.Add(Path.GetFileName(item));
loaddata(item, tree);
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
//当treeview选择项改变后触发的事件
if (e.Node != null)
{
if (e.Node.Tag != null)
{
textBox1.Text = File.ReadAllText(e.Node.Tag.ToString(), Encoding.Default);
}
}
|