<%@ Page ContentType="text/html" Language="VB" %>
<%@ Import Namespace="System.IO" %>
<%@ Register TagPrefix="ie" Namespace="Microsoft.Web.UI.WebControls"
Assembly="Microsoft.Web.UI.WebControls" %>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim root As String = Server.MapPath("./doc")
SetNewNode(root, tree.Nodes)
End Sub ' 指定されたパス(フォルダ)を親とするサブフォルダ/ファイルを ' ツリーに追加
Sub SetNewNode(path As String, nodes As TreeNodeCollection)
' 指定されたパス(path)直下のサブフォルダを取得。
' 順に現在のノード配下に子ノードとして追加
Dim aryDir() As String = Directory.GetDirectories(path)
For Each strDir As String In aryDir ' 親フォルダのパス文字列を切り捨てることで、 ' ファイル名のみを取得
Dim strPath As String = strDir.Substring(path.Length + 1)
Dim node As New TreeNode()
node.Type = "Folder"
node.Text = strPath
' カレント・フォルダ配下のサブフォルダ/ファイルを再帰的に追加
SetNewNode(strDir, node.Nodes)
nodes.Add(node)
Next
' 指定されたパス(path)直下に格納されたファイルを取得。
' 上記同様、順に現在のノード配下に子ノードとして追加
Dim aryFle() As String = Directory.GetFiles(path)
For Each strFle As String In aryFle
Dim strPath As String = strFle.Substring(path.Length + 1)
Dim node As New TreeNode()
node.Type = "File"
node.Text = strPath
' ファイルの場合には、該当ファイルへのリンクを生成
node.NavigateUrl = strFle.Substring(Server.MapPath("./").Length)
nodes.Add(node)
Next
End Sub
</script>
<html>
<head>
<title>任意のフォルダ配下の一覧を表示する</title>
</head>
<body>
<form runat="Server">
<ie:TreeView id="tree" runat="Server"
SystemImagesPath="/webctrl_client/1_0/treeimages/">
<ie:TreeNodeType Type="Folder"
ExpandedImageUrl="/webctrl_client/1_0/images/folderopen.gif"
ImageUrl="/webctrl_client/1_0/images/folder.gif" />
<ie:TreeNodeType Type="File"
ImageUrl="/webctrl_client/1_0/images/html.gif" />
</ie:TreeView>
</form>
</body>
</html>