日期:2014-05-17  浏览次数:20794 次

为什么后面的代码都不能执行
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
  public partial class Form2 : Form
  {
  public Form2()
  {
  InitializeComponent();
  TreeNode root = new TreeNode();
  root.Text = "我的电脑";
  root.Tag = "Root";

  this.treeView1.Nodes.Add(root);

  TreeNode fakeNode = new TreeNode();

  root.Nodes.Add(fakeNode);
  }//只能运行到这里 求解释

  private void ShowDrives(TreeNode node)
  {
  DriveInfo[] drives = DriveInfo.GetDrives();

  foreach (DriveInfo drive in drives)
  {
  TreeNode n = new TreeNode();
  n.Text = drive.Name;
  n.Tag = drive;

  if (drive.IsReady && (drive.RootDirectory.GetDirectories().Length > 0 || drive.RootDirectory.GetFiles().Length > 0))
  {
  TreeNode fakeNode = new TreeNode();
  n.Nodes.Add(fakeNode);
  }

  node.Nodes.Add(n);
  }
  }

  private void ShowRootDirectoriesAndFiles(TreeNode node)
  {
  DriveInfo drive = node.Tag as DriveInfo;

  if (drive != null)
  {
  DirectoryInfo[] directories = drive.RootDirectory.GetDirectories();
  FileInfo[] files = drive.RootDirectory.GetFiles();

  BindDirectoriesAndFiles(node, directories, files);
  }
  }

  private void ShowDirectoriesAndFiles(TreeNode node)
  {
  DirectoryInfo directory = node.Tag as DirectoryInfo;

  if (directory != null)
  {
  DirectoryInfo[] directories = directory.GetDirectories();
  FileInfo[] files = directory.GetFiles();

  BindDirectoriesAndFiles(node, directories, files);
  }
  }

  private void BindDirectoriesAndFiles(TreeNode node, DirectoryInfo[] directories, FileInfo[] files)
  {
  foreach (DirectoryInfo dir in directories)
  {
  TreeNode n = new TreeNode();
  n.Text = dir.Name;
  n.Tag = dir;

  try
  {
  if (dir.GetDirectories().Length > 0 || dir.GetFiles().Length > 0)
  {
  TreeNode fakeNode = new TreeNode();
  n.Nodes.Add(fakeNode);
  }
  }
  catch
  {
  }

  node.Nodes.Add(n);
  }

  foreach (FileInfo file in files)
  {
  TreeNode n = new TreeNode();
  n.Text = file.Name;
  n.Tag = file;

  node.Nodes.Add(n);