社区
C#
帖子详情
如何在TREEVIEW中一次显示C:
xj5162004
2004-05-07 12:32:58
能显示所有目录、子目录和文件。
再此感谢!
...全文
60
6
打赏
收藏
如何在TREEVIEW中一次显示C:
能显示所有目录、子目录和文件。 再此感谢!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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中去。
vfp6.0系统免费下载
值得庆幸的是,运行时刻文件只需下载
一次
。 问题 2-3: 与 Visual FoxPro 5.0
中
的拖放相比,OLE 拖放有什么不同? 答案: 在此版本之前,Visual FoxPro 支持表单与表单上控件之间的拖放。这类拖放只能用于 Visual ...
毕业设计 基于ASP.NET在线知识管理系统成品课程讲解
该课程
中
应用到MasterPage模板页、
TreeView
导航控件、用户自定义控件、数据验证控件等ASP.NET服务器端控件。 该课程可以作为课程设计、大作业或毕业设计作品的思路参考。 、注意:学院会员可以免费学习部分课程 ,...
giCell zlg-gui
时器、任务延迟、
一次
定时(此时需定义一超时后执行的过程);此服务任务的优先 级由其客户任务的最高优先级驱动,随之动态改变,以消除隐式优先级反转现象; 5. 提供任务(Task)、互斥锁(Mutex)、信号量...
vc++ 开发实例源码包
请求的长度在第一个INT
中
指定. 2) 每个服务器通常会向多种客户提供服务, 例如, TS要同时向CP, NP提供服务, CP要向NP和其他CP提供服务, 同时还是其他CP, TS, SP的客户. 3) 每个服务器为客户服务时, 通常是长期的, 会...
C#
110,539
社区成员
642,577
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章