sharesetup.aspx.vb

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

VB
140
字号
Imports System.IO
Imports System.Data
Imports System.Type
Imports System.Collections.Generic
Imports System.IO.Compression
Imports Microsoft.Win32
Imports System.Diagnostics
Partial Class FileManager_ShareSetup
    Inherits WebDiskBasePage

    '填充选定的目录和文件到GridView中
    Private Sub FillGridView(ByVal list As List(Of String))

        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("Path", 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")))

        '装载文件类型的图标
        Dim tempGif As String
        For Each strName As String In list
            If File.Exists(strName) Then             '共享文件
                dataRow = dt.NewRow

                dataRow("Name") = Path.GetFileName(strName)
                dataRow("Path") = Path.GetDirectoryName(strName)
                tempGif = FileDirServer.GetGifByFileName(strName)
                If File.Exists(Server.MapPath(tempGif)) Then
                    dataRow("Extension") = tempGif
                Else
                    dataRow("Extension") = Data.C_smilefacegifMapPath
                End If
                Dim FileInfo As FileInfo = New FileInfo(strName)
                dataRow("Length") = FileDirServer.FormatSize(FileInfo.Length)
                dataRow("CreationTime") = FileInfo.CreationTime
                dt.Rows.Add(dataRow)
            ElseIf Directory.Exists(strName) Then       '共享目录
                Dim Index As Integer
                Index = strName.LastIndexOf("\")
                dataRow = dt.NewRow
                If Index > 0 Then
                    dataRow("Name") = Mid(strName, Index + 2)
                Else
                    dataRow("Name") = strName
                End If
                dataRow("Path") = Path.GetDirectoryName(strName)
                dataRow("Extension") = Data.C_foldergifMapPath '显示目录图标
                Dim DirectoryInfo As DirectoryInfo = New DirectoryInfo(strName)
                dataRow("Length") = FileDirServer.FormatSize(FileDirServer.GetDirectorySize(DirectoryInfo))
                dataRow("CreationTime") = DirectoryInfo.CreationTime
                dt.Rows.Add(dataRow)
            End If
        Next

        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Dim list As List(Of String) = CType(Session("SelectItem"), List(Of String))
            FillGridView(list)
            Session("SelectItem") = Nothing
        End If
    End Sub

    Protected Sub butSet_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butSet.Click
        '测试获取GridView中控件的数据
        Dim strName As String
        Dim chkDel As CheckBox
        Dim chkEdit As CheckBox
        Dim txtToUser As TextBox
        Dim txtPwd As TextBox
        Dim strPath As String
        Dim dgi As GridViewRow
        Dim dsFile As New DataSet
        Dim dsDir As New DataSet
        ' ds.Tables.Add("File")
        'ds.Tables.Add("Dir")
        '通过代码把ds中表的架构做好,避免ds推断
        Try

            ShareXmlServer.CheckShareFile(Data.C_ShareDirXmlMapPath, False)
            ShareXmlServer.CheckShareFile(Data.C_ShareFileXmlMapPath, True)
            dsFile.ReadXml(Server.MapPath(Data.C_ShareFileXmlMapPath))
            dsDir.ReadXml(Server.MapPath(Data.C_ShareDirXmlMapPath))

            For Each dgi In GridView1.Rows
                strName = CType(dgi.FindControl("Name"), Literal).Text
                chkDel = CType(dgi.FindControl("chkDel"), CheckBox)
                chkEdit = CType(dgi.FindControl("chkEdit"), CheckBox)
                txtToUser = CType(dgi.FindControl("txtToUser"), TextBox)
                txtPwd = CType(dgi.FindControl("txtPwd"), TextBox)
                strPath = CType(dgi.FindControl("Path"), Literal).Text
                Dim Value() As String = {Session("User").ToString, txtPwd.Text, strName, strPath, chkEdit.Checked, chkDel.Checked, txtToUser.Text}
                '判断是目录还是文件
                Dim tempName As String = Path.Combine(strPath, strName)
                If File.Exists(tempName) Then
                    dsFile.Tables("File").Rows.Add(Value)
                ElseIf Directory.Exists(tempName) Then
                    dsDir.Tables("Dir").Rows.Add(Value)
                End If
            Next
            Application.Lock()
            dsFile.Tables("File").WriteXml(Server.MapPath(Data.C_ShareFileXmlMapPath))
            dsDir.Tables("Dir").WriteXml(Server.MapPath(Data.C_ShareDirXmlMapPath))
            Application.UnLock()
            Me.StatusMessage.Text = "设置共享成功"
        Catch ex As Exception
            Me.StatusMessage.Text = "设置共享失败" & ex.Message
        End Try
    End Sub
    '建立ShareFile.xml文件
    Private Sub Ini_ShareFile(ByVal ds As DataSet)
        '"~/XML/ShareFile.xml"文件不存在则建立该文件,在设置共享时建立问题
        Dim dt As DataTable = ds.Tables.Add("File")
        dt.Columns.Add("User")
        dt.Columns.Add("Pwd")
        dt.Columns.Add("Name")
        dt.Columns.Add("Path")
        dt.Columns.Add("EnableEdit")
        dt.Columns.Add("EnableDel")
        dt.Columns.Add("EnableUser")
    End Sub
    '建立ShareDir.xml文件
    Private Sub Ini_ShareDir(ByVal ds As DataSet)
        '"~/XML/ShareDir.xml"文件不存在则建立该文件,在设置共享时建立问题
        Dim dt As DataTable = ds.Tables.Add("Dir")
        dt.Columns.Add("User")
        dt.Columns.Add("Pwd")
        dt.Columns.Add("Name")
        dt.Columns.Add("Path")
        dt.Columns.Add("EnableEdit")
        dt.Columns.Add("EnableDel")
        dt.Columns.Add("EnableUser")
    End Sub
End Class

⌨️ 快捷键说明

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