vb2008 文件目录相关(2)

本文介绍使用VB.NET进行文件操作的方法,包括独立存储文件的创建与读写、文本文件的创建与读取、二进制文件的创建与读取等。通过实例展示了如何利用System.IO命名空间下的类来实现这些功能。

  以下摘自《vb2008开发经验与实战宝典》 

  '读取和写入独立存储文件

    Private Sub Button34_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button34.Click

        Dim MyStore As System.IO.IsolatedStorage.IsolatedStorageFile = _

           System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForAssembly()

        '在独立存储中创建一个文件夹

        MyStore.CreateDirectory("MyFolder")

        '创建独立存储文件

        Dim MyStream As System.IO.Stream = New System.IO. _

                 IsolatedStorage.IsolatedStorageFileStream("MyFile1.txt", _

                 System.IO.FileMode.Create, MyStore)

        'Dim MyStream As System.IO.Stream = New System.IO. _

        '        IsolatedStorage.IsolatedStorageFileStream("MyFile2.txt", _

        '        System.IO.FileMode.Create, MyStore)

        'Dim MyStream As System.IO.Stream = New System.IO. _

        '        IsolatedStorage.IsolatedStorageFileStream("MyFile3.txt", _

        '        System.IO.FileMode.Create, MyStore)

        'Dim MyStream As System.IO.Stream = New System.IO. _

        '        IsolatedStorage.IsolatedStorageFileStream("MyFile4.txt", _

        '        System.IO.FileMode.Create, MyStore)

        'Dim MyStream As System.IO.Stream = New System.IO. _

        '        IsolatedStorage.IsolatedStorageFileStream("MyFile5.txt", _

        '        System.IO.FileMode.Create, MyStore)

        Dim MyWriter As New System.IO.StreamWriter(MyStream, _

                                                 System.Text.Encoding.UTF8)

        MyWriter.WriteLine("C:\文件夹中的文件包括:")

        For Each MyFileName As String In System.IO.Directory.GetFiles("C:")

            MyWriter.WriteLine(MyFileName)

        Next

        MyWriter.Flush()

        MyWriter.Close()

        MyStream.Close()

        Dim MyInfo As String = ""

        '独立存储区中的信息

        MyInfo += "当前尺寸: " + MyStore.CurrentSize.ToString() + vbCrLf

        MyInfo += "存储区范围: " + MyStore.Scope.ToString() + vbCrLf

        MyInfo += "包含的文件有:" + vbCrLf

        Dim MyFiles() As String = MyStore.GetFileNames("*.*")

        For Each MyFile As String In MyFiles

            MyInfo += MyFile + vbCrLf

        Next

        MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

    '读取和写入一个文本文件

    Private Sub Button35_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button35.Click

        '创建(写入)一个文本文件

        Dim MyStream As New System.IO.FileStream("MyText.txt", System.IO.FileMode.Create)

        Dim MyWriter As New System.IO.StreamWriter(MyStream, System.Text.Encoding.UTF8)

        MyWriter.WriteLine("C:\文件夹中的文件包括:")

        For Each MyFileName As String In System.IO.Directory.GetFiles("C:")

            MyWriter.WriteLine(MyFileName)

        Next

        MyWriter.Flush()

        MyWriter.Close()

        MyStream.Close()

        '读取一个文本文件          

        Dim MyReader As New System.IO.StreamReader("MyText.txt", System.Text.Encoding.UTF8)

        Dim MyInfo As String = MyReader.ReadToEnd()

        MyReader.Close()

        MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

    '读取和写入一个二进制文件

    Private Sub Button36_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button36.Click

        Try

            '创建(写入)一个二进制文件

            Dim MyStream As New System.IO.FileStream("MyBinary.dat", System.IO.FileMode.Create)

            Dim MyWriter As New System.IO.BinaryWriter(MyStream)

            For i As Integer = 0 To 30 Step +1

                MyWriter.Write(i)

            Next

            MyWriter.Close()

            MyStream.Close()

            '读取一个二进制文件

            MyStream = New System.IO.FileStream("MyBinary.dat", System.IO.FileMode.Open, System.IO.FileAccess.Read)

            Dim MyReader As New System.IO.BinaryReader(MyStream)

            Dim MyInfo As String = ""

            For i As Integer = 0 To 30 Step +1

                MyInfo += MyReader.ReadInt32().ToString() + vbCrLf

            Next

            MyReader.Close()

            MyStream.Close()

            MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Catch ex As Exception

            MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)

        End Try

    End Sub

End Class

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值