一个简单的c#资源管理器.docxVIP

  • 1
  • 0
  • 约1.74千字
  • 约 3页
  • 2024-01-02 发布于天津
  • 举报

双击TreeView加在窗体中加入一个TreeView、RichTextBox、TextBox,布局随便调整吧,入

双击TreeView加

用如下代码相应替代,一个简单的资源管理器就有了。

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.IO;

namespaceMyExplore

{

///summary

///DescriptionofForm1.

////summary

publicpartialclassForm1:Form

{

TreeNoderootnode=newTreeNode();

stringstr=〃〃;

publicForm1()

{

InitializeComponent();

rootnode.Text=〃〃;

〃获得整个系统磁盘驱动

DriveInfo[]drivers=DriveInfo.GetDrives();

//将盘符名加载到TreeView的顶级节点

foreach(DriveInfodriverindrivers)

{

TreeNodetreenode=newTreeNode();

treenode.Text=driver.Name;

rootnode.Nodes.Add(treenode);

}

rootnode.Text=System;

this.treeView1.Nodes.Add(rootnode);

}

voidTreeView1AfterSelect(objectsender,TreeViewEventArgse)

{

if(e.Node.Text!=System)

{

str=

e.Node.FullPath.Replace(@System,string.Empty).Replace(@\\,@\);

this.textBox1.Text=str;

DirectoryInfodirectoryInfo=newDirectoryInfo(str);try{

foreach(DirectoryInfochildDirectoryInfoindirectoryInfo.GetDirectories())

{

TreeNodedirectoryNode=newTreeNode(childDirectoryInfo.Name.ToString());

directoryNode.Tag=childDirectoryInfo.Name.ToString();

e.Node.Nodes.Add(directoryNode);

}

this.treeView1.Refresh();

}

catch(System.IO.DirectoryNotFoundExceptionee)

{

this.textBox1.Text=ee.ToString();

}

try{

StringBuildersb=newStringBuilder();

DirectoryInfodirectoryInfo1=newDirectoryInfo(str);

foreach(FileInfofileInfoindirectoryInfo1.GetFiles(*.*)){

sb.AppendLine(fileInfo.Name);

}

this.richTextBox1.Text=sb.ToString();

}

catch(System.IO.FileNotFoundExceptionee)

{

this.textBox1.Text=ee.ToString();

}

}

}

}

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档