Imports System.IO Public Class DisplayChapter12Dir Inherits System.Web.UI.Page Protected WithEvents IMG2 As System.Web.UI.HtmlControls.HtmlImage Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents lbl1 As System.Web.UI.WebControls.Label Protected WithEvents lbl2 As System.Web.UI.WebControls.Label Protected WithEvents lbl3 As System.Web.UI.WebControls.Label Protected WithEvents lbl4 As System.Web.UI.WebControls.Label Protected WithEvents lbl5 As System.Web.UI.WebControls.Label Protected WithEvents lbl6 As System.Web.UI.WebControls.Label Protected WithEvents lbl7 As System.Web.UI.WebControls.Label Protected WithEvents lblproperty1 As System.Web.UI.WebControls.Label Protected WithEvents lblproperty2 As System.Web.UI.WebControls.Label Protected WithEvents lblproperty3 As System.Web.UI.WebControls.Label Protected WithEvents lblproperty4 As System.Web.UI.WebControls.Label Protected WithEvents lblproperty5 As System.Web.UI.WebControls.Label Protected WithEvents lblproperty6 As System.Web.UI.WebControls.Label Protected WithEvents lblproperty7 As System.Web.UI.WebControls.Label Protected WithEvents lblproperty8 As System.Web.UI.WebControls.Label Protected WithEvents lblproperty9 As System.Web.UI.WebControls.Label Protected WithEvents lbl8 As System.Web.UI.WebControls.Label Protected WithEvents lblproperty10 As System.Web.UI.WebControls.Label Protected WithEvents lbl9 As System.Web.UI.WebControls.Label Protected WithEvents lbl10 As System.Web.UI.WebControls.Label Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox Protected WithEvents ImageButton1 As System.Web.UI.WebControls.ImageButton Protected WithEvents lblSubdirectoriesList As System.Web.UI.WebControls.Label Protected WithEvents lblDirectoryProperties As System.Web.UI.WebControls.Label Protected WithEvents tblSubDirectory As System.Web.UI.HtmlControls.HtmlTable Protected WithEvents lblFiles As System.Web.UI.WebControls.Label Protected WithEvents tblDirectory As System.Web.UI.HtmlControls.HtmlTable Protected WithEvents tblFiles As System.Web.UI.HtmlControls.HtmlTable Protected WithEvents lblFileList As System.Web.UI.WebControls.Label Protected WithEvents lblSubdirectory As System.Web.UI.WebControls.Label Protected WithEvents Message As System.Web.UI.WebControls.Label #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Dim MyPath As String Dim MyLength, MyFileName, MyFileExt, MyLink As String Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here HideAll() End Sub Public Function DisplayDirectoryInfo(ByVal MyPath) ' Enter the code snippet here End Function Private Sub ImageButton1_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click ShowAll() MyPath = TextBox1.Text ' Trim off a \ in case the user entered the \ If MyPath.EndsWith("\") Then MyPath = MyPath.TrimEnd("\") End If Call DisplayDirectoryInfo(MyPath) Call DisplaySubdirectoryList(MyPath) Call DisplayFileList(MyPath) End Sub Public Function DirSize(ByVal MyDD As DirectoryInfo) As Long Dim Size As Long = 0 ' Get file sizes Dim MyFileInfoSize As FileInfo() = MyDD.GetFiles() Dim MyFile As FileInfo For Each MyFile In MyFileInfoSize Size += MyFile.Length Next MyFile ' Get subdirectory sizes Dim MyDirInfo As DirectoryInfo() = MyDD.GetDirectories() Dim MyDir As DirectoryInfo For Each MyDir In MyDirInfo Size += DirSize(MyDir) Next MyDir Return Size End Function Public Function DisplaySubdirectoryList(ByVal MyPath) ' Create the DirectoryInfo object Dim DDL As DirectoryInfo = _ New DirectoryInfo(MyPath) Dim SubDir As DirectoryInfo Dim myDirName, MySubDir As String ' Display directory list except for FrontPage folders ' that start with an underscore (_) For Each SubDir In DDL.GetDirectories() myDirName = SubDir.Name If myDirName.Substring(0, 1) = "_" Then Else If SubDir.Name = "Users" Then ' Display Users subdirectory as a hyperlink MySubDir &= GetHyperlink(MyPath, "Users", 67, "Directory") Else MySubDir &= SubDir.Name & "
" End If lblSubdirectoriesList.Text = MySubDir End If Next End Function Public Function DisplayFileList(ByVal MyPath) 'Create the FileInfo object Dim DFL As DirectoryInfo = _ New DirectoryInfo(MyPath) Dim MyFile As FileInfo If DFL.Name = "ProductThumbs" Or DFL.Name = "ProductThumbnails" _ Or DFL.Name = "CatThumbnails" Or DFL.Name = "SubCatThumbnails" Then ' Display the image thumbnails as image hyperlinks For Each MyFile In DFL.GetFiles Select Case MyFile.Extension Case ".db" ' Windows XP creates a .db file when thumbnails are previewed ' Don't display the thumbs.db file Case Else ' Display the images as hyperlinks, but not the image MyLink &= _ GetHyperlink(MyPath, MyFile.Name, MyFile.Length, "Images") End Select Next Else For Each MyFile In DFL.GetFiles Select Case MyFile.Extension Case ".jpg", ".gif" ' Display the images as hyperlinks, but not the image MyLink &= _ GetHyperlink(MyPath, MyFile.Name, MyFile.Length, "ImageList") Case ".aspx" ' Display only ASP.NET pages as hyperlinks MyLink &= _ GetHyperlink(MyPath, MyFile.Name, MyFile.Length, "Length") Case ".vb", ".asmx", ".ascx", ".resx", ".css", _ ".xsd", ".xsx", ".asax", ".config", ".webinfo", ".vbproj" ' Don't display these files Case Else MyLink &= MyFile.Name & "
" ' Display the file name, but as plain text - not a hyperlink End Select Next End If lblFileList.Text = MyLink End Function Public Function HideAll() tblDirectory.Visible = False tblFiles.Visible = False ' Reset the label control so it doesn't redisplay old data lblSubdirectoriesList.Text = "" lblFileList.Text = "" End Function Public Function ShowAll() tblDirectory.Visible = True tblFiles.Visible = True ' Reset the label control so it doesn't redisplay old data lblSubdirectoriesList.Text = "" lblFileList.Text = "" End Function Public Function GetHyperlink(ByVal TargetURL, ByVal MyFileName, ByVal MyLength, ByVal MyExtra) If TargetURL.Substring(0, 19) = "c:\inetpub\wwwroot\" Then ' replace the directory path with the http:// ' so that the Web pages will appear in the browser TargetURL = _ TargetURL.Replace(MyPath.Substring(0, 19), "http://localhost/") Else End If Dim MyDisplayContent, MyResponse, MyEnding As String Select Case MyExtra Case "Images" ' Creates the image for the hyperlink MyDisplayContent = "" MyEnding = "  " Case "Length" ' Appends the file size onto the link MyDisplayContent = MyFileName MyEnding = GetLength(MyLength) Case Else ' Just displays the file name MyDisplayContent = MyFileName MyEnding = "
" End Select ' Creates the hyperlink MyResponse = _ "" & _ MyDisplayContent & "" & MyEnding Return MyResponse End Function Public Function GetLength(ByVal MyLength) ' Returns the file size Dim MyResponse As String If MyLength > 1024000 Then MyResponse = _ Format(((MyLength) / 1024000), "###0.00") & " MB" Else MyResponse = _ Format(((MyLength) / 1024), "###0.00") & " KB" End If Return " (" & MyResponse & ")
" End Function End Class