文件操作

21 篇文章 0 订阅

Public Class Form1

    Private Sub btnCreatePath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreatePath.Click
        Dim sPath As String = ""
        Dim sFile As String = ""
        Dim sPathFile As String = ""
        sPath = txtPath.Text.ToString & "\"
        sFile = txtFile.Text.ToString
        txtMsg.Text = ""

        sPathFile = sPath & sFile
        Try
            System.IO.Directory.CreateDirectory(sPath)
        Catch ex As Exception
            txtMsg.Text = "Create Directory " & sPath & " fail"
        End Try
    End Sub

    Private Sub btnCreateFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateFile.Click
        Dim sPath As String = ""
        Dim sFile As String = ""
        Dim sPathFile As String = ""
        sPath = txtPath.Text.ToString & "\"
        sFile = txtFile.Text.ToString
        txtMsg.Text = ""

        sPathFile = sPath & sFile
        Dim fsFileStream As System.IO.FileStream = Nothing
        Dim swStreamWriter As System.IO.StreamWriter = Nothing

        Try
            fsFileStream = New System.IO.FileStream(sPathFile, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite)
            swStreamWriter = New System.IO.StreamWriter(fsFileStream)
            swStreamWriter.WriteLine("Test StreamWriter Start")
            swStreamWriter.WriteLine("System.IO.FileMode.Create")
            swStreamWriter.WriteLine("Test StreamWriter Start")
            swStreamWriter.WriteLine()
        Catch ex As Exception
            txtMsg.Text = "Create " & sPathFile & " fail"
        Finally
            If Not swStreamWriter Is Nothing Then
                swStreamWriter.Close()
            End If
            If Not fsFileStream Is Nothing Then
                fsFileStream.Close()
            End If
        End Try
    End Sub

    Private Sub btnAppendFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAppendFile.Click
        Dim sPath As String = ""
        Dim sFile As String = ""
        Dim sPathFile As String = ""
        sPath = txtPath.Text.ToString & "\"
        sFile = txtFile.Text.ToString
        txtMsg.Text = ""

        sPathFile = sPath & sFile
        Dim fsFileStream As System.IO.FileStream = Nothing
        Dim swStreamWriter As System.IO.StreamWriter = Nothing

        Try
            fsFileStream = New System.IO.FileStream(sPathFile, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite)
            swStreamWriter = New System.IO.StreamWriter(fsFileStream)
            swStreamWriter.WriteLine("Test StreamWriter Start")
            swStreamWriter.WriteLine("System.IO.FileMode.Append")
            swStreamWriter.WriteLine("Test StreamWriter Start")
            swStreamWriter.WriteLine()
        Catch ex As Exception
            txtMsg.Text = "Append " & sPathFile & " fail"
        Finally
            If Not swStreamWriter Is Nothing Then
                swStreamWriter.Close()
            End If
            If Not fsFileStream Is Nothing Then
                fsFileStream.Close()
            End If
        End Try
    End Sub


    Private Sub btnExistsPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExistsPath.Click
        Dim sPath As String = ""
        sPath = txtPath.Text.ToString & "\"
        txtMsg.Text = ""

        If System.IO.Directory.Exists(sPath) = True Then
            txtMsg.Text = "Exists " & sPath
        Else
            txtMsg.Text = "Not exists " & sPath
        End If
    End Sub

    Private Sub btnExistsFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExistsFile.Click
        Dim sPath As String = ""
        Dim sFile As String = ""
        Dim sPathFile As String = ""
        sPath = txtPath.Text.ToString & "\"
        sFile = txtFile.Text.ToString
        txtMsg.Text = ""

        If System.IO.File.Exists(sPathFile) = True Then
            txtMsg.Text = "Exists " & sPathFile
        Else
            txtMsg.Text = "Not exists " & sPathFile
        End If
    End Sub

    Private Sub btnDeleteFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteFile.Click
        Dim sPath As String = ""
        Dim sFile As String = ""
        Dim sPathFile As String = ""
        sPath = txtPath.Text.ToString & "\"
        sFile = txtFile.Text.ToString
        txtMsg.Text = ""

        sPathFile = sPath & sFile
        Try
            System.IO.File.Delete(sPathFile)
        Catch ex As Exception
            txtMsg.Text = "Delete " & sPathFile & " fail"
        End Try


    End Sub

    Private Sub btnMoveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveFile.Click
        Dim sPath As String = ""
        Dim sFile As String = ""
        Dim sPathFile As String = ""
        Dim sToPath As String = ""
        Dim sToFile As String = ""
        Dim sToPathFile As String = ""

        sPath = txtPath.Text.ToString & "\"
        sFile = txtFile.Text.ToString
        sPathFile = sPath & sFile

        sToPath = txtToPath.Text.ToString & "\"
        sToFile = txtToFile.Text.ToString
        sToPathFile = sToPath & sToFile

        txtMsg.Text = ""

        Try
            System.IO.File.Move(sPathFile, sToPathFile)
        Catch ex As Exception
            txtMsg.Text = "Move " & sPathFile & " fail"
        End Try
    End Sub
End Class

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值