editfile.aspx.vb

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

VB
139
字号
Imports Data
Imports FileDirServer
Imports System.IO
Partial Class FileManager_EditFile
    Inherits WebDiskBasePage
    Protected SaveAsPath As TextBox
    Private UserDir As String


    ' Page Events
    '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    Private Sub Page_Load( _
      ByVal sender As Object, _
      ByVal e As EventArgs) _
      Handles MyBase.Load

        If Not IsPostBack Then
            'Dim UserDirCookie As HttpCookie = Request.Cookies("UserDir")
            '对Cookie进行解码
            'UserDir = Server.UrlDecode(UserDirCookie("UserDir"))
            '服务器基目录不存在,则禁用
            UserDir = Session("UserDir").ToString
            If IsNothing(UserDir) Or Not UserDir.Contains(Session("ServerDir").ToString) Then
                Response.Redirect("/WebDisk/Help/Message_Err.aspx")
                Return
            End If
            Dim filePath As String = Server.UrlDecode(Request.Params("File"))
            If filePath.StartsWith(Session("ServerDir")) Then
                If (Not IsNothing(filePath)) And (filePath.IndexOf("%") = -1) Then

                    If (Request.Params("CreateFile") Is Nothing) Or (Request.Params("CreateFile") = "False") Then

                        Try
                            Dim fileEncoding As System.Text.Encoding = TxtFileEncoding.GetEncoding(filePath, System.Text.Encoding.GetEncoding("GB2312"))
                            Dim reader As New StreamReader(File.Open(filePath, FileMode.Open), fileEncoding)  ' System.Text.Encoding.Default)

                            CodeText.Text = reader.ReadToEnd
                            reader.Close()

                        Catch myException As Exception
                            StatusMessage.Text = filePath
                            StatusMessage.Text &= myException.Message

                        End Try
                    ElseIf Request.Params("CreateFile") = "True" Then
                        SaveAs.Enabled = False
                    End If


                End If

            End If
            '显示编辑文件名,去除服务路径
            txtEditFile.Text = FileDirServer.ShowMapPath(filePath)
        Else
            Me.StatusMessage.Text = String.Empty
        End If
    End Sub



    ' Control Events
    '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    Protected Sub Back_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Back.Click

        '返回文件所在的目录
        Dim filePath As String = Server.UrlDecode(Request.Params("File"))

        If Not IsNothing(filePath) And filePath <> "\" Then

            Dim lastSlashIndex As Integer = filePath.LastIndexOf("\")
            Dim folderPath As String = filePath.Substring(0, lastSlashIndex)
            If folderPath.StartsWith(Session("UserDir").ToString) Then
                Response.Redirect("MainFile.aspx?Folder=" & folderPath)
            Else
                Response.Redirect("MainFile.aspx?Folder=" & Session("CurrentPath"))
            End If
        Else
            Response.Redirect("MainFile.aspx")

        End If

    End Sub


    Protected Sub SaveBtn_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveBtn.ServerClick
        Dim filePath As String = Server.UrlDecode(Request.Params("File"))
        WriteText(filePath)
    End Sub
    Protected Sub SaveAs_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveAs.Click
        Dim filePath As String
        filePath = Path.Combine(Session("CurrentPath").ToString, SaveAsTxt.Value)
        WriteText(filePath)
    End Sub
    Protected Sub WriteText(ByVal FilePath As String)
        ' Dim UserDirCookie As HttpCookie = Request.Cookies("UserDir")
        '对Cookie进行解码
        UserDir = Session("UserDir").ToString
        Dim FlagWrite As Boolean
        ' Save the text to the specified file (the file is created
        ' ex-novo even if it already exists)
        StatusMessage.Visible = True
        If IsNothing(filePath) Then
            StatusMessage.Text = "用户目录有误,请确认用户路径"
            Return
        ElseIf filePath.IndexOf("%") <> -1 Then
            StatusMessage.Text = "地址览中文件没有以中文显示"
            Return
        End If
        If filePath.StartsWith(UserDir) Then
            Try
                If GetDirectorySize(UserDir) < Data.C_LargeUseDir Then
                    FlagWrite = True
                Else
                    FlagWrite = False
                    StatusMessage.Text = "你的 " & UserDir & "用户目录已经大于允许的空间,请删除一些文件后再存盘"
                End If
                If FlagWrite Then
                    '当文件共享时,需要锁定写
                    Application.Lock()
                    Dim writer As StreamWriter = New StreamWriter(File.Open(FilePath, FileMode.Create), System.Text.Encoding.Default)
                    writer.Write(CodeText.Text)
                    writer.Close()
                    writer = Nothing
                    Application.UnLock()
                    StatusMessage.Text = "文件成功保存"
                End If
            Catch myException As Exception
                StatusMessage.Text = myException.Message
            End Try
        Else
            StatusMessage.Text = "只能保存在用户目录、或其子目录下"
        End If
    End Sub

End Class

⌨️ 快捷键说明

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