⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filemanage.ascx.vb

📁 如果不使用IIS,请先运行 XSP.exe,待提示已侦听 8080端口后
💻 VB
字号:
Imports System.IO
Partial Class fileManage
    Inherits System.Web.UI.UserControl

    ''' <summary>
    ''' 选择文件
    ''' 参数为文件的虚拟路径
    ''' </summary>
    ''' <param name="file"></param>
    ''' <remarks></remarks>
    Public Event SelFile(ByVal file As String)
    ''' <summary>
    ''' 选择目录
    ''' 参数为目录的虚拟路径
    ''' </summary>
    ''' <param name="dir"></param>
    ''' <remarks></remarks>
    Public Event SelDir(ByVal dir As String)


    Private _rootpath As String

    Public Property RootPath() As String
        Get
            Return ViewState("_rootpath")
        End Get
        Set(ByVal value As String)
            ViewState("_rootpath") = value
        End Set
    End Property


    Private _showfile As Boolean=True 
    ''' <summary>
    ''' 是否显示文件
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ShowFile() As Boolean
        Get
            Return _showfile
        End Get
        Set(ByVal value As Boolean)
            _showfile = value
        End Set
    End Property



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _rootpath = ViewState("_rootpath")
        If Not Page.IsPostBack Then

            If Not _showfile Then
                lblftitle.Visible = False
                dltFiles.Visible = False
            End If

            If String.IsNullOrEmpty(_rootpath) Then
                Return
            End If
            FillTree(_rootpath)
        End If
    End Sub

    Private Sub FillTree(ByVal path As String)
        Dim root As String = Server.MapPath(path)
        If Right(root, 1) = "\" Then
            root = root.Substring(0, root.Length - 1)
        End If

        Dim rootnode As New TreeNode(IO.Path.GetFileName(root))

        rootnode.Expand()

        trvPath.Nodes.Add(rootnode)

        AddNode(rootnode, root)

        trvPath.Nodes(0).Select()

        trvPath_SelectedNodeChanged(Me, New EventArgs)

    End Sub
    Private Sub AddNode(ByVal node As TreeNode, ByVal path As String)
        Dim dir As String() = Directory.GetDirectories(path)
        For Each d As String In dir
            If Right(d, 1) = "\" Then
                d = d.Substring(0, d.Length - 1)
            End If

            Dim n As New TreeNode(IO.Path.GetFileName(d))
            node.ChildNodes.Add(n)
            n.Collapse()
            AddNode(n, d)
        Next


    End Sub

    Protected Sub trvPath_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles trvPath.SelectedNodeChanged
        Dim n As TreeNode = trvPath.SelectedNode
        If n Is Nothing Then Return
        Dim d As String = GetDirPath(n)


        If dltFiles.Visible Then

            Dim f As String() = Directory.GetFiles(d)
            dltFiles.DataSource = f
            dltFiles.DataBind()
        End If

        If Not TypeOf (sender) Is UserControl Then
            RaiseEvent SelDir(VDir)
        End If


    End Sub

    Private Function GetDirPath(ByVal node As TreeNode) As String

        Dim cur As String = node.ValuePath.Replace(trvPath.Nodes(0).Text, "")
        Dim r As String = Server.MapPath(_rootpath)
        If Right(r, 1) = "\" Then
            r = r.Substring(0, r.Length - 1)
        End If
        cur = r & cur

        Return cur
    End Function

    Protected Sub dltFiles_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dltFiles.ItemDataBound
        Dim f As String = e.Item.DataItem.ToString()

        Try
            Dim file As New FileInfo(f)

            Dim lblname As Label = e.Item.FindControl("lblfilename")
            lblname.Text = file.Name

            Dim lblsize As Label = e.Item.FindControl("lblfilesize")
            lblsize.Text = (file.Length / 1000).ToString() & "K"


            'Dim linksel As LinkButton = e.Item.FindControl("linksel")
            'linksel.CommandArgument = file.Name

            Dim lblcreatedate As Label = e.Item.FindControl("lblcreatedate")
            lblcreatedate.Text = file.LastWriteTime.ToString()

            Dim img As Image = e.Item.FindControl("fileimg")

            Dim imgfile As String = file.Extension.Substring(1) & ".gif"
            img.ImageUrl = "~/filemanageimg/icons/" & imgfile


        Catch ex As Exception

        End Try

    End Sub
    Protected Sub dltFiles_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dltFiles.ItemCommand
        dltFiles.SelectedIndex = e.Item.ItemIndex

        RaiseEvent SelFile(VFile)

    End Sub

    ''' <summary>
    ''' 选择的文件的物理路径
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property PhyFile() As String
        Get

            If dltFiles.SelectedItem Is Nothing Then
                Return String.Empty
            End If

            Dim lblname As Label = dltFiles.SelectedItem.FindControl("lblfilename")

            Dim file As String = lblname.Text

            Dim path As String = GetDirPath(trvPath.SelectedNode)

            Return IO.Path.Combine(path, file)

        End Get
    End Property

    ''' <summary>
    ''' 选择的文件的虚拟路径
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property VFile() As String
        Get
            If dltFiles.SelectedItem Is Nothing Then
                Return String.Empty
            End If

            Dim p As String = PhyFile

            p = p.Replace(Server.MapPath("~/"), "~/")
            p = p.Replace("\", "/")


            Return p

        End Get
    End Property


    ''' <summary>
    ''' 选择的文件夹物理路径
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property PhyDir() As String
        Get
            If trvPath.SelectedNode Is Nothing Then
                Return ""
            End If

            Return (GetDirPath(trvPath.SelectedNode))


        End Get
    End Property


    ''' <summary>
    ''' 选择的文件夹的虚拟路径
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property VDir() As String
        Get

            If trvPath.SelectedNode Is Nothing Then
                Return String.Empty
            End If

            Dim p As String = PhyDir

            p = p.Replace(Server.MapPath("~/"), "~/")
            p = p.Replace("\", "/")


            Return p
        End Get
        
    End Property




End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -