如何在TREEVIEW中一次显示C:

xj5162004 2004-05-07 12:32:58
能显示所有目录、子目录和文件。
再此感谢!
...全文
38 6 点赞 打赏 收藏 举报
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xj5162004 2004-05-14
好象在我的机子运行只能显示C:盘下的文件,而不能显示文件夹下的文件???
  • 打赏
  • 举报
回复
xj5162004 2004-05-13
谢谢,向你学习。
  • 打赏
  • 举报
回复
chenyuming2004 2004-05-12
TreeNode tn;
foreach(string di in Directory.GetDirectories(@"c:\"))
{
tn=new TreeNode();
tn.Text=di;
treeView1.Nodes.Add(tn);
}
foreach(string di in Directory.GetFiles(@"c:\"))
{
tn=new TreeNode();
tn.Text=di;
treeView1.Nodes.Add(tn);
}
  • 打赏
  • 举报
回复
peterli1976 2004-05-12
程序將c:所有文件寫到treeview1,如果選擇點擊目錄則將目錄中所有文件列出在treeview2
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;


namespace WindowsApplication10
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.TreeView treeView2;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.treeView1 = new System.Windows.Forms.TreeView();
this.treeView2 = new System.Windows.Forms.TreeView();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(192, 320);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 32);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// treeView1
//
this.treeView1.CheckBoxes = true;
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(8, 16);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(240, 296);
this.treeView1.TabIndex = 0;
this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.treeview1checked);
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.checkfilelist);
//
// treeView2
//
this.treeView2.CheckBoxes = true;
this.treeView2.ImageIndex = -1;
this.treeView2.Location = new System.Drawing.Point(280, 16);
this.treeView2.Name = "treeView2";
this.treeView2.SelectedImageIndex = -1;
this.treeView2.Size = new System.Drawing.Size(248, 296);
this.treeView2.TabIndex = 1;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(80, 360);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(440, 22);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
this.ClientSize = new System.Drawing.Size(544, 389);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.treeView2);
this.Controls.Add(this.treeView1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{

button1.Visible=! button1.Visible;
treeView1.Nodes.Clear();
string[] sdrivers=Environment.GetLogicalDrives();
foreach (string rootDirectoryName in sdrivers)
{
//確定只有c盤顯示
if (rootDirectoryName !=@"C:\")
continue;
try
{

DirectoryInfo dir = new DirectoryInfo(rootDirectoryName);
dir.GetDirectories();
TreeNode ndroot = new TreeNode(rootDirectoryName);
treeView1.Nodes.Add(ndroot);
GetSubDirectoryNodes(ndroot,ndroot.Text); //遍歷子節點
}
catch (Exception ie)
{
MessageBox.Show(ie.Message);
}
}
}
private void GetSubDirectoryNodes(TreeNode parentNode,string fullName)
{
try
{

DirectoryInfo dir = new DirectoryInfo(fullName);
DirectoryInfo[] dirSubs = dir.GetDirectories();
foreach (DirectoryInfo dirSub in dirSubs)
{
if ((dirSub.Attributes & FileAttributes.Hidden) !=0)
{
continue;
}
TreeNode subNode = new TreeNode(dirSub.FullName);
parentNode.Nodes.Add(subNode);
GetSubDirectoryNodes(subNode,dirSub.FullName);
}
}

catch
{
}




}

private void checkfilelist(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
try
{

treeView2.Nodes.Clear();
DirectoryInfo dir = new DirectoryInfo(e.Node.Text);

FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
TreeNode fileNode = new TreeNode(file.Name);
treeView2.Nodes.Add(fileNode);
}

}
catch
{
}

}

private void treeview1checked(object sender, System.Windows.Forms.TreeViewEventArgs e)
{

textBox1.Text+=e.Node.Text;

}






}
}
  • 打赏
  • 举报
回复
xj5162004 2004-05-12
谢谢,我的意思是将C:盘下所有的文件夹和文件,包括文件夹的子文件夹、文件,子文件夹下的子文件夹、文件.....就是一次加入所有的文件夹和文件。
  • 打赏
  • 举报
回复
chenyuming2004 2004-05-07
Directory.GetFiles("c:\\");
Directory.GetDirectories("c:\\");
一个是得到C盘根目录下的所有文件
一个是得到所有根目录下的所有文件夹
你把这些添加到treeview中去。
  • 打赏
  • 举报
回复
相关推荐
发帖
C#
加入

10.6w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2004-05-07 12:32
社区公告

让您成为最强悍的C#开发者