本文整理汇总了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
示例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
输出:
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