浏览整个服务器的目录:protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { foreach (System.IO.DriveInfo drive in DriveInfo.GetDrives()) { TreeNode node = new TreeNode(); node.Value = drive.Name; if (drive.IsReady) { node.Text = drive.Name + " free"; LoadDirectories(node, drive.Name); } else node.Text = drive.Name + " not ready"; this.TreeView1.Nodes.Add(node); } } this.TreeView1.CollapseAll();
} private void LoadDirectories(TreeNode parent, string path) { DirectoryInfo directory = new DirectoryInfo(path); try { foreach (DirectoryInfo d in directory.GetDirectories()) { TreeNode node = new TreeNode(d.Name, d.FullName); parent.ChildNodes.Add(node); LoadDirectories(node, d.FullName); }
} catch (System.UnauthorizedAccessException e) { parent.Text += " (Acess Denied)"; } catch (IOException e) { parent.Text += "(UNknown error:" + e.Message + ")"; } }