本文整理匯總了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