使用Treeview類比作業系統的檔案系統

來源:互聯網
上載者:User
default.aspx 代碼:

<%@ 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.