社区
C#
帖子详情
如何在TREEVIEW中一次显示C:
xj5162004
2004-05-07 12:32:58
能显示所有目录、子目录和文件。
再此感谢!
...全文
85
6
打赏
收藏
如何在TREEVIEW中一次显示C:
能显示所有目录、子目录和文件。 再此感谢!
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
6 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
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中去。
把盘符下的所有文件生成
TreeView
把盘符下的文件全部遍历出来,然后生成
TreeView
,TreeNode给设计出来,首先找到根目录,根目录下又有子目录,然后在到文件。TreeNode是根据这个层次关系
显示
出来的。
WPF
中
TreeView
WPF
中
TREEVIEW
的使用,注意与FORM的区别
装入整个 c 盘目录到
treeview
(4KB)...
装入整个 c 盘目录到
treeview
(4KB)
C#无限级绑定
TreeView
.txt
C#无限级绑定,代码简单精练,调用方便,是不可多得的好东东
C#
111,097
社区成员
642,554
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章