C#窗体程序,做一个如图简单的资源管理器,越简单越好,不需要什么添加文件夹 100
1个回答
展开全部
用如下代码相应替代,一个简单的资源管理器就有了。
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text;
using System.Windows.Forms; using System.IO;
namespace MyExplore {
/// <summary>
/// Description of Form1. /// </summary>
public partial class Form1 : Form {
TreeNode rootnode = new TreeNode(); string str = "";
public Form1() {
InitializeComponent(); rootnode.Text = "";
//获得整个系统磁盘驱动
DriveInfo[] drivers = DriveInfo.GetDrives();
//将盘符名加载到TreeView的顶级节点 foreach (DriveInfo driver in drivers) {
TreeNode treenode = new TreeNode(); treenode.Text = driver.Name; rootnode.Nodes.Add( treenode); }
rootnode.Text = "System";
this.treeView1.Nodes.Add(rootnode);
}
void TreeView1AfterSelect(object sender, TreeViewEventArgs e) {
if( e.Node.Text != "System") { str =
e.Node.FullPath.Replace(@"System",string.Empty).Replace(@"\\",@"\"); this.textBox1.Text = str;
DirectoryInfo directoryInfo = new DirectoryInfo(str); try
{
foreach (DirectoryInfo childDirectoryInfo in directoryInfo.GetDirectories()) {
TreeNode directoryNode = new
TreeNode(childDirectoryInfo.Name.ToString());
directoryNode.Tag = childDirectoryInfo.Name.ToString(); e.Node.Nodes.Add(directoryNode); }
this.treeView1.Refresh(); }
catch ( System.IO.DirectoryNotFoundException ee ) {
this.textBox1.Text = ee.ToString(); }
try {
StringBuilder sb = new StringBuilder();
DirectoryInfo directoryInfo1 = new DirectoryInfo(str); foreach (FileInfo fileInfo in directoryInfo1.GetFiles("*.*")) {
sb.AppendLine(fileInfo.Name); }
this.richTextBox1.Text = sb.ToString(); }
catch( System.IO.FileNotFoundException ee ) {
this.textBox1.Text = ee.ToString(); } } } }
}
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text;
using System.Windows.Forms; using System.IO;
namespace MyExplore {
/// <summary>
/// Description of Form1. /// </summary>
public partial class Form1 : Form {
TreeNode rootnode = new TreeNode(); string str = "";
public Form1() {
InitializeComponent(); rootnode.Text = "";
//获得整个系统磁盘驱动
DriveInfo[] drivers = DriveInfo.GetDrives();
//将盘符名加载到TreeView的顶级节点 foreach (DriveInfo driver in drivers) {
TreeNode treenode = new TreeNode(); treenode.Text = driver.Name; rootnode.Nodes.Add( treenode); }
rootnode.Text = "System";
this.treeView1.Nodes.Add(rootnode);
}
void TreeView1AfterSelect(object sender, TreeViewEventArgs e) {
if( e.Node.Text != "System") { str =
e.Node.FullPath.Replace(@"System",string.Empty).Replace(@"\\",@"\"); this.textBox1.Text = str;
DirectoryInfo directoryInfo = new DirectoryInfo(str); try
{
foreach (DirectoryInfo childDirectoryInfo in directoryInfo.GetDirectories()) {
TreeNode directoryNode = new
TreeNode(childDirectoryInfo.Name.ToString());
directoryNode.Tag = childDirectoryInfo.Name.ToString(); e.Node.Nodes.Add(directoryNode); }
this.treeView1.Refresh(); }
catch ( System.IO.DirectoryNotFoundException ee ) {
this.textBox1.Text = ee.ToString(); }
try {
StringBuilder sb = new StringBuilder();
DirectoryInfo directoryInfo1 = new DirectoryInfo(str); foreach (FileInfo fileInfo in directoryInfo1.GetFiles("*.*")) {
sb.AppendLine(fileInfo.Name); }
this.richTextBox1.Text = sb.ToString(); }
catch( System.IO.FileNotFoundException ee ) {
this.textBox1.Text = ee.ToString(); } } } }
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询