<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="OsFilesytem._default" %>
<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<iewc:TreeView id="TreeView1" runat="server" AutoPostBack="True" BackColor="#EEEEEE"></iewc:TreeView>
</form>
</body>
</HTML>
default.aspx.cs代码:
using System;using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.Web.UI.WebControls;
using System.IO;
namespace OsFilesytem
{
/// <summary>
/// WebForm2 的摘要说明。
/// </summary>
public class _default : System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
TreeNode newnode=new TreeNode();
newnode.Text="我的电脑";
this.TreeView1.Nodes.Add(newnode);
try
{
string[] drives = System.IO.Directory.GetLogicalDrives();
foreach (string str in drives)
{
TreeNode tmpNd;
tmpNd=new TreeNode();
tmpNd.Text=str;
this.TreeView1.Nodes[0].Nodes.Add(tmpNd);
//ProcessDirectory(str,tmpNd);
}
}
catch (System.IO.IOException)
{
this.Response.Write("I/O错误!");
}
catch (System.Security.SecurityException)
{
this.Response.Write("没有访问权限!");
}
}
}
public void ProcessDirectory(string targetDirectory,TreeNode TN)
{
if(targetDirectory.ToString()==@"A:\")
{
return;
}
TreeNode tmpNd;
try
{
// Process the list of files found in the directory
string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
{
tmpNd=new TreeNode();
//只显示文件名
tmpNd.Text=fileName.Substring(fileName.LastIndexOf(@"\")+1);
//显示文件 全名(路径+名称)
//tmpNd.Text=fileName;
TN.Nodes.Add(tmpNd);
}
// Recurse into subdirectories of this directory
string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach(string subdirectory in subdirectoryEntries)
{
tmpNd=new TreeNode();
//只显示文件夹名
tmpNd.Text=subdirectory.Substring(subdirectory.LastIndexOf(@"\")+1);
//显示文件夹 全名(路径+名称)
//tmpNd.Text=subdirectory;
TN.Nodes.Add(tmpNd);
//ProcessDirectory(subdirectory,tmpNd);
}
}
catch
{
}
}
/// <summary>
/// 获得当前结点的全名
/// </summary>
/// <param name="TN">当前结点</param>
/// <returns>全名</returns>
private string GetFullName(TreeNode TN)
{
if(TN.Text=="我的电脑")
{
return "我的电脑";
}
string str_fullname="";
while(((TreeNode)TN.Parent).Text!="我的电脑")
{
str_fullname = TN.Text + @"\" + str_fullname;
TN=(TreeNode)TN.Parent;
}
str_fullname =TN.Text + str_fullname;
return str_fullname;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.TreeView1.Expand += new Microsoft.Web.UI.WebControls.ClickEventHandler(this.TreeView1_Expand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void TreeView1_Expand(object sender, Microsoft.Web.UI.WebControls.TreeViewClickEventArgs e)
{
TreeNode TNS=this.TreeView1.GetNodeFromIndex(e.Node);
foreach(TreeNode TN in TNS.Nodes)
{
this.ProcessDirectory(this.GetFullName(TN),TN);
}
}
}
}
OVER