A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

转自:http://www.jb51.net/article/52282.htm
这篇文章主要介绍了C#实现获取系统目录并以Tree树叉显示的方法,可以加深读者对于C#下数据结构实现方法的认识,需要的朋友可以参考下
本文讲述C#获取Windows系统目录,如何目录遍历以及将信息捆绑在TreeView中显示出来的实现方法,具体实现代码如下:
  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.IO;
  8. namespace 获取系统目录
  9. {
  10. public class Form1 : System.Windows.Forms.Form
  11. {
  12. private System.Windows.Forms.TreeView treeView1;
  13. private System.ComponentModel.IContainer components;
  14. private System.Windows.Forms.Button button2;
  15. private System.Windows.Forms.Label label2;
  16. private System.Windows.Forms.Button button1;
  17. private System.Windows.Forms.TextBox textBox1;
  18. private System.Windows.Forms.Label label1;
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. FillTree(treeView1, true); // 显示所有文件夹以及文件
  23. }
  24. protected override void Dispose( bool disposing )
  25. {
  26. if( disposing )
  27. {
  28. if (components != null)
  29. {
  30. components.Dispose();
  31. }
  32. }
  33. base.Dispose( disposing );
  34. }
  35. #region Windows 窗体设计器生成的代码
  36. private void InitializeComponent()
  37. {
  38. this.textBox1 = new System.Windows.Forms.TextBox();
  39. this.label2 = new System.Windows.Forms.Label();
  40. this.label1 = new System.Windows.Forms.Label();
  41. this.treeView1 = new System.Windows.Forms.TreeView();
  42. this.button1 = new System.Windows.Forms.Button();
  43. this.button2 = new System.Windows.Forms.Button();
  44. this.SuspendLayout();
  45. this.textBox1.Location = new System.Drawing.Point(280, 56);
  46. this.textBox1.Multiline = true;
  47. this.textBox1.Name = "textBox1";
  48. this.textBox1.Size = new System.Drawing.Size(208, 192);
  49. this.textBox1.TabIndex = 3;
  50. this.textBox1.Text = "";
  51. this.label2.AutoSize = true;
  52. this.label2.Location = new System.Drawing.Point(280, 24);
  53. this.label2.Name = "label2";
  54. this.label2.Size = new System.Drawing.Size(116, 17);
  55. this.label2.TabIndex = 4;
  56. this.label2.Text = "文件夹或文件信息:";
  57. this.label1.AutoSize = true;
  58. this.label1.Location = new System.Drawing.Point(16, 24);
  59. this.label1.Name = "label1";
  60. this.label1.Size = new System.Drawing.Size(153, 17);
  61. this.label1.TabIndex = 5;
  62. this.label1.Text = "请选择一个文件夹或文件:";
  63. this.treeView1.ImageIndex = -1;
  64. this.treeView1.Location = new System.Drawing.Point(16, 56);
  65. this.treeView1.Name = "treeView1";
  66. this.treeView1.SelectedImageIndex = -1;
  67. this.treeView1.Size = new System.Drawing.Size(184, 192);
  68. this.treeView1.TabIndex = 6;
  69. this.button1.Location = new System.Drawing.Point(216, 104);
  70. this.button1.Name = "button1";
  71. this.button1.Size = new System.Drawing.Size(48, 32);
  72. this.button1.TabIndex = 7;
  73. this.button1.Text = "显示";
  74. this.button1.Click += new System.EventHandler(this.button1_Click);
  75. this.button2.Location = new System.Drawing.Point(216, 168);
  76. this.button2.Name = "button2";
  77. this.button2.Size = new System.Drawing.Size(48, 32);
  78. this.button2.TabIndex = 8;
  79. this.button2.Text = "清空";
  80. this.button2.Click += new System.EventHandler(this.button2_Click);
  81. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  82. this.ClientSize = new System.Drawing.Size(504, 277);
  83. this.Controls.Add(this.button2);
  84. this.Controls.Add(this.button1);
  85. this.Controls.Add(this.treeView1);
  86. this.Controls.Add(this.label1);
  87. this.Controls.Add(this.label2);
  88. this.Controls.Add(this.textBox1);
  89. this.Name = "Form1";
  90. this.Text = "Form1";
  91. this.ResumeLayout(false);
  92. }
  93. #endregion
  94. [STAThread]
  95. static void Main()
  96. {
  97. Application.Run(new Form1());
  98. }
  99. // 填充目录和文件到 TreeView 控件中
  100. // isSource 表示是否显示文件
  101. private void FillTree(TreeView treeView,bool isSource)
  102. {
  103. treeView.Nodes.Clear(); // 清空
  104. // 获取系统上的所有逻辑驱动器
  105. string[] strDrives = Environment.GetLogicalDrives();
  106. foreach(string rootDirectoryName in strDrives)
  107. {
  108. try
  109. {
  110. // 获取驱动器顶级目录列表
  111. DirectoryInfo dir = new DirectoryInfo(rootDirectoryName);

  112. // 如果获得的目录信息正确,则将它添加到 TreeView 控件中
  113. if (dir.Exists == true)
  114. {
  115. TreeNode newNode = new TreeNode(rootDirectoryName);
  116. treeView.Nodes.Add(newNode);
  117. if (isSource)
  118. {
  119. GetSubDirectoryNodes(newNode, newNode.Text, true);
  120. }
  121. else
  122. {
  123. GetSubDirectoryNodes(newNode, newNode.Text, false);
  124. }
  125. }
  126. }
  127. catch(Exception e)
  128. {
  129. MessageBox.Show(e.Message);
  130. }
  131. }
  132. }
  133. // 遍历子目录
  134. private void GetSubDirectoryNodes(TreeNode parentNode, string fullName, bool getFileNames)
  135. {
  136. DirectoryInfo dir = new DirectoryInfo(fullName);
  137. DirectoryInfo[] subDirs = dir.GetDirectories();
  138. // 为每一个子目录添加一个子节点
  139. foreach(DirectoryInfo subDir in subDirs)
  140. {
  141. // 不显示隐藏文件夹
  142. if((subDir.Attributes & FileAttributes.Hidden) != 0)
  143. {
  144. continue;
  145. }
  146. TreeNode subNode = new TreeNode(subDir.Name);
  147. parentNode.Nodes.Add(subNode);
  148. // 递归调用GetSubDirectoryNodes
  149. GetSubDirectoryNodes(subNode, subDir.FullName, getFileNames);
  150. }
  151. // 获取目录中的文件
  152. if(getFileNames)
  153. {
  154. FileInfo[] files = dir.GetFiles();
  155. foreach(FileInfo file in files)
  156. {
  157. TreeNode fileNode = new TreeNode(file.Name);
  158. parentNode.Nodes.Add(fileNode);
  159. }
  160. }
  161. }
  162. private void button1_Click(object sender, System.EventArgs e)
  163. {
  164. try
  165. {
  166. TreeNode selectedNode = treeView1.SelectedNode;
  167. DirectoryInfo info = new DirectoryInfo(selectedNode.FullPath);
  168. string [] strArray = new string[4];
  169. strArray[0] = "完整路径名 : "+ info.FullName;
  170. strArray[1] = "创建时间 : "+ info.CreationTime.ToString();
  171. strArray[2] = "上次访问时间 : "+ info.LastAccessTime.ToString();
  172. strArray[3] = "上次写入时间 : "+ info.LastWriteTime.ToString();
  173. textBox1.Lines = strArray;
  174. }
  175. catch(Exception exc)
  176. {
  177. MessageBox.Show(exc.Message);
  178. }
  179. }
  180. private void button2_Click(object sender, System.EventArgs e)
  181. {
  182. textBox1.Text = "";
  183. }
  184. }
  185. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马