当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET FileStream.Seek方法代码示例

本文整理汇总了VB.NET中System.IO.FileStream.Seek方法的典型用法代码示例。如果您正苦于以下问题:VB.NET FileStream.Seek方法的具体用法?VB.NET FileStream.Seek怎么用?VB.NET FileStream.Seek使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.FileStream的用法示例。


在下文中一共展示了FileStream.Seek方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: FStream

' 导入命名空间
Imports System.IO
Imports System.Text

Class FStream

    Shared Sub Main()

        Const fileName As String = "Test#@@#.dat"

        ' Create random data to write to the file.
        Dim dataArray(100000) As Byte
        Dim randomGenerator As New Random()
        randomGenerator.NextBytes(dataArray)

        Dim fileStream As FileStream = _
            new FileStream(fileName, FileMode.Create)
        Try

            ' Write the data to the file, byte by byte.
            For i As Integer = 0 To dataArray.Length - 1
                fileStream.WriteByte(dataArray(i))
            Next i

            ' Set the stream position to the beginning of the stream.
            fileStream.Seek(0, SeekOrigin.Begin)

            ' Read and verify the data.
            For i As Integer = 0 To _
                CType(fileStream.Length, Integer) - 1

                If dataArray(i) <> fileStream.ReadByte() Then
                    Console.WriteLine("Error writing data.")
                    Return
                End If
            Next i
            Console.WriteLine("The data was written to {0} " & _
                "and verified.", fileStream.Name)
        Finally
            fileStream.Close()
        End Try
    
    End Sub
End Class
开发者ID:VB.NET开发者,项目名称:System.IO,代码行数:44,代码来源:FileStream.Seek

示例2: FSSeek

' 导入命名空间
Imports System.IO

Public Class FSSeek
    Public Shared Sub Main()
        Dim offset As Long
        Dim nextByte As Integer

        ' alphabet.txt contains "abcdefghijklmnopqrstuvwxyz"
        Using fs As New FileStream("c:\temp\alphabet.txt", FileMode.Open, FileAccess.Read)

            For offset = 1 To fs.Length
                fs.Seek(-offset, SeekOrigin.End)
                Console.Write(Convert.ToChar(fs.ReadByte()))
            Next offset
            Console.WriteLine()

            fs.Seek(20, SeekOrigin.Begin)

            nextByte = fs.ReadByte()
            While (nextByte > 0)
                Console.Write(Convert.ToChar(nextByte))
                nextByte = fs.ReadByte()
            End While
            Console.WriteLine()

        End Using
    End Sub
End Class
开发者ID:VB.NET开发者,项目名称:System.IO,代码行数:29,代码来源:FileStream.Seek

输出:

zyxwvutsrqponmlkjihgfedcba
uvwxyz

示例3: Module1

' 导入命名空间
Imports System.IO

Module Module1

    Sub Main()
        Dim FileSt As FileStream = New FileStream("test.dat", FileMode.Create, FileAccess.Write, FileShare.Write)

        Try
            FileSt.Lock(0, 100)
            Console.WriteLine("Locked")
        Catch Ex As Exception
            Console.WriteLine(Ex.Message)
        End Try

        Try
            FileSt.Unlock(0, 100)
            Console.WriteLine("Unlocked")
        Catch Ex As Exception
            Console.WriteLine(Ex.Message)
        End Try

        Dim Values As Byte()
        Values = New Byte() {1, 2, 3, 4, 5}

        Try
            FileSt.Seek(0, SeekOrigin.Begin)
            FileSt.Write(Values, 0, 5)
            Console.WriteLine("Successfully updated file")
        Catch Ex As Exception
            Console.WriteLine(Ex.Message)
        End Try

    End Sub

End Module
开发者ID:VB程序员,项目名称:System.IO,代码行数:36,代码来源:FileStream.Seek


注:本文中的System.IO.FileStream.Seek方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。