C# 文件读取(二)

    将我的电脑中的文件夹信息显示到TreeView控件上。 方法很多种,下面这种方法添加了我的文档。

   

 1 public partial class Form1 : Form

 2     {

 3         public Form1()

 4         {

 5             InitializeComponent();

 6         }

 7 

 8         private void Form1_Load(object sender, EventArgs e)

 9         {

10             BindTreeView1();            

11         }

12 

13         private void BindTreeView1() {

14             //添加我的电脑

15             TreeNode rootNode = new TreeNode("我的电脑");

16             rootNode.Tag = "我的电脑";

17             rootNode.Text = "我的电脑";

18             this.directoryTree.Nodes.Add(rootNode);

19 

20             //我的文档

21             string myDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

22             TreeNode DocNode = new TreeNode(myDocuments);

23             DocNode.Text = "我的文档";

24             DocNode.Tag = "我的文档";

25             rootNode.Nodes.Add(DocNode);

26             DocNode.Nodes.Add("");

27 

28             //循环便利计算机中所以的逻辑驱动器

29             foreach (string drive in Environment.GetLogicalDrives())

30             {

31                 DriveInfo dir = new DriveInfo(drive);

32                 switch (dir.DriveType)

33                 {

34                     case DriveType.Fixed:

35                         TreeNode tNode = new TreeNode(dir.Name.Split(':')[0]);

36                         tNode.Name = dir.Name;

37                         tNode.Tag = tNode.Name;

38                         directoryTree.Nodes.Add(tNode);

39                         tNode.Nodes.Add("");

40                         break;

41                 }

42             }

43             rootNode.Expand();

44         }

45 

46         private void directoryTree_AfterExpand(object sender, TreeViewEventArgs e)

47         {

48             e.Node.Expand();

49         }

50 

51         private void directoryTree_BeforeExpand(object sender, TreeViewCancelEventArgs e)

52         {

53             TreeViewItems.Add(e.Node);

54         }

55     }

56 

57     public static class TreeViewItems {

58 

59         public static void Add(TreeNode e) {

60         

61             if(e.Tag.ToString() != "我的电脑"){

62 

63                 e.Nodes.Clear();

64 

65                 TreeNode tNode = e;

66                 string path = tNode.Name;

67 

68                 if (e.Tag.ToString() == "我的文档")

69                 {

70                     path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

71                 }

72 

73                string [] dics=  Directory.GetDirectories(path);

74                 foreach (string dic in dics)

75                 {

76                     TreeNode subNode = new TreeNode(new DirectoryInfo(dic).Name);

77                     subNode.Name = new DirectoryInfo(dic).FullName;

78                     subNode.Tag = subNode.Name;

79                     tNode.Nodes.Add(subNode);

80                     subNode.Nodes.Add("");

81                 }

82             }

83         }

84     }
显示文件夹

 

你可能感兴趣的:(文件读取)