遍历指定目录(文件夹),并在Datalist列出

时间:2022-04-06 13:12:04

页面:

<%@ Page CodeBehind="Show_Forder.aspx.vb" Language="vb" AutoEventWireup="false" Inherits="_3Gyears.Show_Forder" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <title>获取目录</title>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
  <LINK href="css/index.css" type="text/css" rel="stylesheet">
  <style type="text/css">A:link {
 COLOR: #666666; TEXT-DECORATION: none
}
A:visited {
 COLOR: #666666; TEXT-DECORATION: none
}
A:hover {
 COLOR: #000000; TEXT-DECORATION: none
}
A:active {
 TEXT-DECORATION: none
}
  </style>
 </HEAD>
 <body onblur="self.focus()">
  <form id="Myform" runat="server">
   <table cellSpacing="1" cellPadding="3" width="100%" bgColor="#006699" border="0">
    <tr>
     <td align="center" height="24"><span class="baise">文件目录表 [您要选的类型:<asp:label id="ToplblClass" runat="server"></asp:label>&nbsp;]
      </span>
     </td>
    </tr>
    <tr>
     <td bgColor="#f3f3f3">
      <table cellSpacing="0" cellPadding="0" width="100%" border="0">
       <tr>
        <td><asp:datalist id="ShowClass" runat="server" RepeatDirection="Horizontal">
          <itemtemplate>
           <table width="100%">
            <tr>
             <td>
              &nbsp;
              <asp:ImageButton ID="Imgbut" ImageAlign="AbsMiddle" imageUrl="images/FolderClose.jpg" runat="server"></asp:ImageButton>
              <asp:Label runat="server" text='<%# DataBinder.Eval(Container.DataItem,"FolderName")%>' ForeColor="#FF0000" ID="Label1" />
              <asp:Label runat="server" Visible=false text='<%# DataBinder.Eval(Container.DataItem,"FolderUrl")%>' ID="lblUrl" ForeColor="#FF0000" />
             </td>
            </tr>
           </table>
          </itemtemplate>
         </asp:datalist></td>
       </tr>
      </table>
     </td>
    </tr>
    <tr>
     <asp:panel id="TopTool" runat="server">
      <TD bgColor="#f3f3f3">
       <TABLE cellSpacing="0" cellPadding="0" width="100%" border="0">
        <TR>
         <TD width="55%">当前目录为:
          <asp:label id="UpForder" runat="server"></asp:label></TD>
         <TD align="right" width="45%"><A href="javascript:history.go(-1)"><IMG height="18" src="images/up.jpg" width="18" align="absMiddle" border="0">
           后退一层</A>&nbsp;&nbsp;<A id="msgTop" href="#" runat="server" onserverclick="TopFolder">&nbsp;<IMG height="16" src="images/topfolder.jpg" width="20" align="absMiddle" border="0">
           返回顶层</A> &nbsp;<A id="MsgAll" href="#" runat="server" onserverClick="SelectAll"></A>&nbsp;&nbsp;<A id="MsgDel" href="#" runat="server" onserverClick="DelFile"><IMG height="18" hspace="3" src="images/del.jpg" width="18" align="absMiddle" border="0"><FONT color="#ff0000">删除</FONT></A>&nbsp;</TD>
        </TR>
       </TABLE>
      </TD>
     </asp:panel></tr>
    <tr>
     <td bgColor="#ffffff"><asp:datalist id="ShowImg" runat="server" RepeatDirection="Horizontal" Width="100%" RepeatColumns="5">
       <itemtemplate>
        <table align="center" cellpadding="4" cellspacing="1" bgcolor="#CCCCCC">
         <tr>
          <td align="center" bgcolor="#FFFFFF">
           <asp:ImageButton Runat="server" ImageUrl='<%# GetImg( DataBinder.Eval(Container.DataItem,"Img"),"images/OrderFile.jpg")%>' ID="GetImgname" width=128 height=128>
           </asp:ImageButton>
           <asp:Label runat="server" Visible="false" text='<%# DataBinder.Eval(Container.DataItem,"Img")%>' ID="lblUrl" ForeColor="#FF0000" /></td>
         </tr>
         <tr>
          <td align="center" bgcolor="#F8F8F8"><%# DataBinder.Eval(Container.DataItem,"ImgName")%>
           <asp:CheckBox ID="chkExport" Runat="server" ForeColor="#FF0000" /></td>
         </tr>
        </table>
       </itemtemplate>
      </asp:datalist></td>
    </tr>
   </table>
   <asp:panel id="ShowErr" runat="server" Visible="false">
    <TABLE height="450" width="100%" align="center">
     <TR>
      <TD vAlign="middle" align="center">
       <asp:Label id="msg" runat="server" ForeColor="#FF0000"></asp:Label></TD>
     </TR>
    </TABLE>
   </asp:panel></form>
 </body>
</HTML>

 

Imports System.IO
Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Public Class Show_Forder
    Inherits System.Web.UI.Page

#Region " Web ´°ÌåÉè¼ÆÆ÷Éú³ÉµÄ´úÂë "

    '¸Ãµ÷ÓÃÊÇ Web ´°ÌåÉè¼ÆÆ÷Ëù±ØÐèµÄ¡£
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents ShowClass As System.Web.UI.WebControls.DataList
    Protected WithEvents UpForder As System.Web.UI.WebControls.Label
    Protected WithEvents ShowImg As System.Web.UI.WebControls.DataList
    Protected WithEvents msgTop As System.Web.UI.HtmlControls.HtmlAnchor
    Protected WithEvents msg As System.Web.UI.WebControls.Label
    Protected WithEvents Button As System.Web.UI.WebControls.Button
    Protected WithEvents ShowErr As System.Web.UI.WebControls.Panel
    Protected WithEvents MsgAll As System.Web.UI.HtmlControls.HtmlAnchor
    Protected WithEvents MsgDel As System.Web.UI.HtmlControls.HtmlAnchor
    Protected WithEvents TopTool As System.Web.UI.WebControls.Panel
    Protected WithEvents ToplblClass As System.Web.UI.WebControls.Label
    Protected WithEvents lblzjl As System.Web.UI.WebControls.Label
    Protected WithEvents intPagesize As System.Web.UI.WebControls.Label
    Protected WithEvents intcurrindex As System.Web.UI.WebControls.Label
    Protected WithEvents intRecordCount As System.Web.UI.WebControls.Label
    Protected WithEvents txtCurrindex As System.Web.UI.WebControls.TextBox
    Protected WithEvents ShowPage As System.Web.UI.WebControls.Panel
    Protected WithEvents msbfirst As System.Web.UI.HtmlControls.HtmlAnchor
    Protected WithEvents msgUp As System.Web.UI.HtmlControls.HtmlAnchor
    Protected WithEvents msgDown As System.Web.UI.HtmlControls.HtmlAnchor
    Protected WithEvents msgLast As System.Web.UI.HtmlControls.HtmlAnchor
    Protected WithEvents Msg2 As System.Web.UI.WebControls.Label

    '×¢Òâ: ÒÔÏÂռλ·ûÉùÃ÷ÊÇ Web ´°ÌåÉè¼ÆÆ÷Ëù±ØÐèµÄ¡£
    '²»ÒªÉ¾³ý»òÒƶ¯Ëü¡£
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: ´Ë·½·¨µ÷ÓÃÊÇ Web ´°ÌåÉè¼ÆÆ÷Ëù±ØÐèµÄ
        '²»ÒªÊ¹ÓôúÂë±à¼­Æ÷ÐÞ¸ÄËü¡£
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'ÔÚ´Ë´¦·ÅÖóõʼ»¯Ò³µÄÓû§´úÂë
        If Request("TheUrl") = "" Or Request("TheType") = "" Or Request("TheControl") = "" Then
            ShowErr.Visible = True
            msg.Text = "²ÎÊý³ö´í,ÇëÖØÊÔ!"
            TopTool.Visible = False
        Else
            If Not IsPostBack Then
                Dim ThisFolder As String = Request("ThisFolder")
                If ThisFolder <> "" Then
                    ThisFolder = ThisFolder.Substring(0, ThisFolder.LastIndexOf("/"))

                    If Directory.Exists(Server.MapPath(ThisFolder)) = True Then '¼ì²â·¾¶ÊÇ·ñ´æÔÚ
                        DirFile(ThisFolder, ShowClass)
                        DirImg(ThisFolder)
                    Else
                        DirFile(Request("TheUrl"), ShowClass)
                        DirImg(Request("TheUrl"))
                    End If
                Else
                    DirFile(Request("TheUrl"), ShowClass)
                    DirImg(Request("TheUrl"))
                End If
                ToplblClass.Text = Request("TheType").Replace("|", " ¡¢")
                MsgDel.Attributes.Add("onclick", "return confirm('ÄúÕæµÄҪɾ³ýÑ¡ÖеÄÎļþÂð£¿/n/nÈç¹ûÊý¾Ý¿âÖдæÓдËÏîÊý¾Ý,ÄÇôҳÃ潫»á³ö´í!');")
                MsgAll.InnerHtml = "<img src='images/select.jpg' width='19' height='19' hspace='3' border='0' align='absmiddle'>È«Ñ¡"
            End If
        End If
    End Sub

    Public Function DirFile(ByVal TheUrl As String, ByVal TheDatalis As System.Web.UI.WebControls.DataList)
        UpForder.Text = TheUrl
        Dim fileList As String() = System.IO.Directory.GetDirectories(Server.MapPath(TheUrl))  'Áгö´ËĿ¼ÏµÄ×ÓÎļþ¼Ð
        Dim dtList As New DataTable
        dtList.Columns.Add(New DataColumn("FolderUrl", GetType(String)))
        dtList.Columns.Add(New DataColumn("FolderName", GetType(String)))
        Dim i As Integer
        For i = 0 To UBound(fileList)
            Dim FileSplit() As String = Split(fileList(i), "/")
            Dim FileName As String = FileSplit(FileSplit.Length - 1) '»ñÈ¡Îļþ¼ÐÃû
            Dim drNew As DataRow = dtList.NewRow()
            drNew("FolderName") = FileName
            drNew("FolderUrl") = TheUrl & "/" & FileName
            dtList.Rows.Add(drNew)
        Next
        If dtList.Rows.Count = 0 Then
            'ShowErr.Visible = True
            'msg.Text = "´ËÎļþ¼ÐûÓÐÏàÓ¦Îļþ,ÇëÑ¡Ôñ±ðµÄÎļþ¼Ð!"
        Else
            TheDatalis.Visible = True
            dtList.DefaultView.Sort = "FolderName desc"
            TheDatalis.DataSource = dtList
            TheDatalis.DataBind()
        End If
    End Function

    Private Sub DirImg(ByVal TheUrl As String)
        ShowErr.Visible = False
        ShowImg.Visible = True
        Dim tmpFiler As String = Request("TheType")
        Dim ImgType As String() = Split(tmpFiler, "|")
        Dim dtList As New DataTable '´´½¨Êý¾Ý±í
        dtList.Columns.Add(New DataColumn("Img", GetType(String))) '´´½¨Êý¾ÝÁÐ"img"
        dtList.Columns.Add(New DataColumn("ImgName", GetType(String))) '´´½¨Êý¾ÝÁÐ"ImgName"
        dtList.Columns.Add(New DataColumn("ImgTime", GetType(DateTime))) '´´½¨Êý¾ÝÁÐ"ImgName"
        '»ñÈ¡Ö¸¶¨Îļþ¼Ð
        Dim fileList As String() = System.IO.Directory.GetFiles(Server.MapPath(TheUrl))
        Dim dirinfo = New DirectoryInfo(Server.MapPath(TheUrl))
        '»ñÈ¡ÆäÖеÄÎļþ
        Dim arrFileInfo As FileInfo() = dirinfo.GetFiles()
        '
        Dim i As Integer
        For i = 0 To arrFileInfo.Length - 1
            Dim strName As String = arrFileInfo(i).Name.ToString()
            Dim strTime As DateTime = arrFileInfo(i).LastWriteTime
            'ÊÇ·ñΪͼƬ
            Dim tmp As String
            For Each tmp In ImgType
                If strName.IndexOf(tmp) <> -1 Then
                    Dim drNew As DataRow = dtList.NewRow() '´´½¨Êý¾ÝÐÐ
                    drNew("Img") = TheUrl & "/" & strName 'ΪÊý¾Ý"img"ÐÐÌí¼ÓÖµ
                    drNew("ImgName") = strName 'ΪÊý¾Ý"imgName"ÐÐÌí¼ÓÖµ
                    drNew("ImgTime") = strTime
                    dtList.Rows.Add(drNew) '°ÑÊý¾ÝÐÐÌí¼Óµ½Êý¾Ý±í
                End If
            Next
        Next
        If dtList.Rows.Count = 0 Then
            ShowErr.Visible = True
            msg.Text = "µ±Ç°Îļþ¼ÐûÓÐÏàÓ¦Îļþ,ÇëÑ¡ÔñÆäËüµÄÎļþ¼Ð!"
            ShowImg.Visible = False
            MsgDel.Visible = False
            MsgAll.Visible = False
        Else
            MsgDel.Visible = True
            MsgAll.Visible = True
            dtList.DefaultView.Sort = "ImgTime desc"
            Me.ShowImg.DataSource = dtList
            Me.DataBind()
        End If
    End Sub

    Private Sub ShowClass_SelectedCmd(ByVal sender As Object, ByVal e As DataListCommandEventArgs) Handles ShowClass.ItemCommand
        Dim TheUrl As String = CType(e.Item.FindControl("lblUrl"), Label).Text
        DirFile(TheUrl, ShowClass)
        DirImg(TheUrl)

    End Sub

    Sub TopFolder(ByVal s As Object, ByVal e As EventArgs) '·µ»Ø¶¥²ã,ÖØÐÂÔØÈëÊý¾Ý
        If Request("TheUrl") <> "" Then
            DirFile(Request("TheUrl"), ShowClass)
            DirImg(Request("TheUrl"))

            MsgAll.InnerHtml = "<img src='images/select.jpg' width='19' height='19' hspace='3' border='0' align='absmiddle'>È«Ñ¡"
        Else
            ShowErr.Visible = True
            msg.Text = "²ÎÊý³ö´í,ÇëÖØÊÔ!"
            TopTool.Visible = False
        End If
    End Sub
    Private Sub ShowImg_SelectedCmd(ByVal sender As Object, ByVal e As DataListCommandEventArgs) Handles ShowImg.ItemCommand
        Dim Thestr As String = CType(e.Item.FindControl("lblUrl"), Label).Text
        Response.Write("<script language=javascript>window.opener.MyForm." & Request("TheControl") & ".value='" & Thestr & "'</script>") '´«Öµ¸ø¸¸´°
        Response.Write("<script language=javascript>window.close();</script>")
    End Sub

    Public Function GetImg(ByVal ImgType As String, ByVal ImgUrl As String)
        Dim TheImg As String = Microsoft.VisualBasic.Right(ImgType, 3)
        If TheImg = "gif" Or TheImg = "jpg" Or TheImg = "png" Or TheImg = "bmp" Then
            Return ImgType
        Else
            Return ImgUrl
        End If
    End Function

    Sub selectAll(ByVal s As Object, ByVal e As EventArgs)
        Dim TheDatalistItem As DataListItem
        Dim ChkExp As System.Web.UI.WebControls.CheckBox
        If MsgAll.InnerHtml = "<img src='images/select.jpg' width='19' height='19' hspace='3' border='0' align='absmiddle'>È«Ñ¡" Then
            For Each TheDatalistItem In ShowImg.Items
                ChkExp = TheDatalistItem.FindControl("chkExport")
                ChkExp.Checked = True
            Next
            MsgAll.InnerHtml = "<img src='images/select.jpg' width='19' height='19' hspace='3' border='0' align='absmiddle'>²»Ñ¡"
        Else
            For Each TheDatalistItem In ShowImg.Items
                ChkExp = TheDatalistItem.FindControl("chkExport")
                ChkExp.Checked = False
            Next
            MsgAll.InnerHtml = "<img src='images/select.jpg' width='19' height='19' hspace='3' border='0' align='absmiddle'>È«Ñ¡"
        End If
    End Sub

    Sub DelFile(ByVal s As Object, ByVal e As EventArgs) 'ɾ³ýÎļþ
        Dim rptItem As DataListItem
        Dim chkExport As System.Web.UI.WebControls.CheckBox
        Dim TheUrl As String
        For Each rptItem In ShowImg.Items
            chkExport = rptItem.FindControl("chkExport")
            If chkExport.Checked Then
                TheUrl = CType(rptItem.FindControl("lblUrl"), Label).Text
                If File.Exists(Server.MapPath(TheUrl)) Then '¼ì²âÎļþÊÇ·ñ´æÔÚ?
                    File.Delete(Server.MapPath(TheUrl)) 'ɾ³ýÎļþ
                End If
            End If
        Next
        DirImg(UpForder.Text)
        MsgAll.InnerHtml = "<img src='images/select.jpg' width='19' height='19' hspace='3' border='0' align='absmiddle'>È«Ñ¡"
    End Sub

End Class