mainfile.aspx.vb

来自「是可以运行的电子光盘 有程序与PPT介绍 对于学习VB。NET的有参考意义」· VB 代码 · 共 660 行 · 第 1/2 页

VB
660
字号
Imports System.IO
Imports System.Data
Imports System.Type
Imports System.Collections.Generic
Imports System.IO.Compression
Imports Microsoft.Win32
Imports System.Diagnostics
'明确根目录和当前目录,UserDir为根目录
Partial Class FileManager_MainFile
    Inherits WebDiskBasePage
    Private CurrentPath As String   '当前目录
    Private UserDir As String '用户根目录
    '在Page_load中,要想保留选种项信息,则不能运行 FillGridView,即不能重新绑定Gridview,否则选种项始终为false
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            '将文件缓存
            '   Response.AddCacheDependency(New CacheDependency(Server.MapPath("~/XML/TextExtend.xml")))

            '服务器基目录不存在,则禁用
            UserDir = Session("UserDir")
            CheckPath(UserDir)
            '排序默认值
            Session("SortName") = "Name"
            Session("SortAscending") = "yes"
            PanelTree.Visible = False
            BindData()
            '设定会话是否通过密码进行,Session("downpass") = True表示已经通过密码验证
            Session("downpass") = True
        Else
            Me.StatusMessage.Text = String.Empty
        End If
    End Sub
    '检查用户目录是否正确,并设置当前路径
    Private Sub CheckPath(ByVal UserDir As String)
        If UserDir = "FALSE" Or IsNothing(UserDir) Then
            Response.Redirect("/WebDisk/Message_Err.aspx")
            Return
        End If
        '若用户目录不存在,自动建立
        If Not Directory.Exists(UserDir) Then
            Directory.CreateDirectory(UserDir)
            CurrentPath = UserDir
        Else
            '根据Folder查询参数定位装载的目录
            CurrentPath = Server.UrlDecode(Request.Params("Folder"))  '有时最后一个汉字丢失
            If IsNothing(CurrentPath) Then
                CurrentPath = UserDir
            ElseIf CurrentPath.IndexOf("%") <> -1 Then   '中文在地址览中有时显示为%e7...格式
                StatusMessage.Text = "地址览中目录没有以中文显示"
                StatusMessage.Visible = True
                CurrentPath = UserDir
            ElseIf Not CurrentPath.StartsWith(UserDir) Then
                StatusMessage.Text = "开始目录不是用户目录,可能在共享目录下" ' & UserDir
                StatusMessage.Visible = True
                CurrentPath = UserDir
            ElseIf Not Directory.Exists(CurrentPath) Then
                StatusMessage.Text = CurrentPath & "目录不存在"
                StatusMessage.Visible = True
                CurrentPath = UserDir
            End If
        End If
        '改变当前路径
        Session("CurrentPath") = CurrentPath
    End Sub

#Region "FillTree,FillGridView,bindData"
    '填充指定目录下的目录和文件到Tree控件
    Private Sub FillTree(ByVal folderPath As String)
        '根据当前目录填充TreeView
        Dim node As New TreeNode
        Try
            TVDir.Nodes.Clear()
            node.Text = FileDirServer.ShowMapPath(Session("UserDir").ToString)
            node.NavigateUrl = "MainFile.aspx" '?Folder=" & Server.UrlEncode(Session("UserDir"))
            node.ImageUrl = Data.C_homegifMapPath
            TVDir.Nodes.Add(node)
            Dim str() As String = Directory.GetDirectories(Session("UserDir").ToString)
            For i As Integer = 0 To str.GetUpperBound(0)
                '调用遍历过程
                AddDirectory(str(i), node)
            Next
            '在TreeView相应Node下加载文件
            AddFile(Session("UserDir").ToString, node)
            TVDir.ExpandDepth = 2
        Catch ex As Exception

        End Try
        node = Nothing
    End Sub
    '填充指定目录下的目录和文件到GridView中
    Private Sub FillGridView(ByVal folderPath As String)
        ' Declare local variables to hold dir and file information
        ' Dim location As String
        Dim parentDir As DirectoryInfo
        Dim childDirs As DirectoryInfo()
        Dim childFiles As FileInfo()

        ' Get dir and file information
        Try
            parentDir = New DirectoryInfo(folderPath)
            childDirs = parentDir.GetDirectories()
            childFiles = parentDir.GetFiles()
        Catch exc As Exception
            StatusMessage.Text = exc.Message
            StatusMessage.Visible = True
            Return
        End Try

        Dim dirInfo As New DirectoryInfo(folderPath)
        Dim Childdir As DirectoryInfo() = dirInfo.GetDirectories()
        Dim dt As DataTable = New DataTable
        Dim dataRow As DataRow
        dt.Columns.Add(New DataColumn("Name", System.Type.GetType("System.String")))
        dt.Columns.Add(New DataColumn("Extension", System.Type.GetType("System.String")))
        dt.Columns.Add(New DataColumn("Length", System.Type.GetType("System.String")))
        dt.Columns.Add(New DataColumn("CreationTime", System.Type.GetType("System.String")))

        '装载目录
        For Each tempdirInfo As DirectoryInfo In Childdir
            dataRow = dt.NewRow
            dataRow(0) = tempdirInfo.Name
            dataRow(1) = Data.C_foldergifMapPath
            dataRow(2) = FileDirServer.FormatSize(FileDirServer.GetDirectorySize(tempdirInfo))
            dataRow(3) = tempdirInfo.CreationTime
            dt.Rows.Add(dataRow)

        Next
        '装载文件
        Dim ChildFile As FileInfo() = dirInfo.GetFiles()
        Dim tempGif As String
        For Each tempFileInfo As FileInfo In ChildFile
            dataRow = dt.NewRow
            dataRow(0) = tempFileInfo.Name
            tempGif = "~/filetype/" & Mid(tempFileInfo.Extension, 2) & ".gif"
            If File.Exists(Server.MapPath(tempGif)) Then
                dataRow(1) = tempGif
            Else
                dataRow(1) = Data.C_smilefacegifMapPath
            End If
            dataRow(2) = FileDirServer.FormatSize(tempFileInfo.Length)
            dataRow(3) = tempFileInfo.CreationTime
            dt.Rows.Add(dataRow)
        Next
        Dim dv As DataView = dt.DefaultView
        If Session("SortAscending") = "yes" Then
            dv.Sort = Session("SortName").ToString + " ASC"
        Else
            dv.Sort = Session("SortName").ToString + " DESC"
        End If
        GridView1.DataSource = dv
        GridView1.DataBind()
        parentDir = Nothing
        childDirs = Nothing
        childFiles = Nothing
        dt = Nothing
    End Sub

    Private Sub BindData()
        FillGridView(Session("CurrentPath").ToString)
        FillTree(Session("UserDir").ToString)
        '在显示当前路径时, 不显示服务目录
        currentPathTxt.Text = FileDirServer.ShowMapPath(Session("CurrentPath").ToString)
    End Sub

#End Region

#Region "创建删除目录文件..."
    Protected Sub CreDir_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles CreDir.Click
        Dim thePath As String
        CurrentPath = Session("CurrentPath")
        thePath = Path.Combine(CurrentPath, funcParam.Value) '判断后自动加入"\"
        Try

            ' Create the directory
            If Directory.Exists(thePath) Then
                StatusMessage.Text = "要创建的目录已经存在,请更换目录名"
                StatusMessage.Visible = True
            Else
                Directory.CreateDirectory(thePath)
                ' Refresh Page
                BindData()
            End If
        Catch exc As Exception
            StatusMessage.Text = exc.Message
            StatusMessage.Visible = True
        End Try
    End Sub
    Protected Sub CreFile_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles CreFile.Click

        ' Build the complete path (current path + new dir name)
        Dim thePath As String
        thePath = Path.Combine(Session("CurrentPath").ToString, funcParam.Value)

        ' If the file already exists, do not go to the text editor
        If File.Exists(thePath) Then
            StatusMessage.Text = "要创建的文件已经存在,请更换文件名"
            StatusMessage.Visible = True
        Else
            Response.Redirect("EditFile.aspx?File=" & thePath & "&CreateFile=True")
        End If
    End Sub
    '删除目录或文件
    Protected Sub del_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles del.Click
        If Me.HaveSelectFileOrDirName() Then
            Dim strFileorDirName As String
            Try
                For Each strFileorDirName In GetSelectFileOrDirName()
                    If Directory.Exists(strFileorDirName) Then
                        Directory.Delete(strFileorDirName, True)
                    Else
                        File.Delete(strFileorDirName)
                    End If
                Next
            Catch ex As Exception
                Me.StatusMessage.Text = "删除失败" & ex.Message
            End Try
            ' Refresh Page
            'Response.Redirect("MainFile.aspx?Folder=" & Server.UrlEncode(CurrentPath))
            BindData()
        Else
            Me.StatusMessage.Text = "请选中文件或目录"
        End If

    End Sub
#End Region

#Region "GridView控件事件"

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim location As String
            Dim hy As HyperLink = CType(e.Row.FindControl("NameLink"), HyperLink)
            CurrentPath = Session("CurrentPath")
            location = Path.Combine(CurrentPath, hy.Text)
            If Directory.Exists(location) Then 'location如果是目录
                hy.NavigateUrl = "MainFile.aspx?Folder=" & Server.UrlEncode(location)
            Else 'location为文件
                hy.NavigateUrl = "Download.aspx?File=" & Server.UrlEncode(location) '对中文进行编码
                Dim EditLink As HyperLink = CType(e.Row.FindControl("EditLink"), HyperLink)
                Dim fi As New FileInfo(location)
                '判断是否是文本文件
                If FileDirServer.IsTextFile(fi.Extension.ToLower, Server.MapPath(Data.C_TextExtendXmlMapPath)) Then
                    EditLink.Visible = True
                    EditLink.NavigateUrl = "EditFile.aspx?File=" & Server.UrlEncode(location)  '对中文进行编码
                End If
            End If

        End If
    End Sub

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "rename" Then
            Dim strFileName As String = e.CommandArgument.ToString
            If funcParam.Value <> String.Empty Then
                Dim Source As String = Path.Combine(Session("CurrentPath").ToString, strFileName)
                Dim Dest As String = Path.Combine(Session("CurrentPath").ToString, funcParam.Value)
                '是否是目录
                Try
                    If Directory.Exists(Source) Then
                        Directory.Move(Source, Dest)
                    End If
                    If File.Exists(Source) Then
                        '判断funcParam.Value有无扩展名
                        If funcParam.Value.Contains(".") And Right(funcParam.Value, 1) <> "." Then
                            File.Move(Source, Dest)
                        Else
                            Dim fi As FileInfo = New FileInfo(Source)
                            If fi.Extension <> String.Empty Then
                                Dest &= fi.Extension
                            End If
                            File.Move(Source, Dest)
                        End If

                    End If
                    BindData()
                Catch ex As Exception

                End Try

            End If
        End If
    End Sub
    '处理GridView的排序事件()
    Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
        WebPageShare.GridViewSort(GridView1, e)
        FillGridView(Session("CurrentPath").ToString)
    End Sub
#End Region

#Region "导航向上或根..."
    '导航到根目录,注意有两种根目录,1是用户根目录,2是共享目录的根目录
    Protected Sub GoRoot_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles GoRoot.Click
        ' Refresh Page
        Session("CurrentPath") = Session("UserDir")
        BindData()
    End Sub

    '重定位到用户登录的根目录
    Protected Sub ibutSelfDir_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibutSelfDir.Click
        Session("UserDir") = FileDirServer.GetUserDir(Session("User"))
        Session("CurrentPath") = Session("UserDir")
        BindData()
    End Sub
    '导航到当前目录的上一级目录
    Protected Sub UpBtn_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles UpBtn.Click
        Dim location As String
        CurrentPath = Session("CurrentPath").ToString
        '判断向上(即目录后退)有无到达根目录
        Dim lastSlashIndex As Integer = CurrentPath.LastIndexOf("\")
        location = CurrentPath.Substring(0, lastSlashIndex)
        If location.Length <= Session("UserDir").Length Then
            location = Session("UserDir").ToString           '用户根目录
        End If
        '改变当前目录
        Session("CurrentPath") = location
        ' Refresh Page
        BindData()
    End Sub
#End Region

#Region "TreeView控件操作代码"
    '将目录strPath中的全部文件
    Public Sub AddFile(ByVal strPath As String, ByVal node As TreeNode)
        '再遍历这个目录下的文件
        Try
            Dim fileNames As String() = Directory.GetFiles(strPath)
            For Each file As String In fileNames
                Dim subtn As TreeNode = New TreeNode()
                Dim strExt As String = Path.GetExtension(file)

⌨️ 快捷键说明

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