本文整理匯總了VB.NET中System.IO.FileStream.FileStream構造函數的典型用法代碼示例。如果您正苦於以下問題:VB.NET FileStream構造函數的具體用法?VB.NET FileStream怎麽用?VB.NET FileStream使用的例子?那麽, 這裏精選的構造函數代碼示例或許可以為您提供幫助。您也可以進一步了解該構造函數所在類System.IO.FileStream
的用法示例。
在下文中一共展示了FileStream構造函數的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: FileStreamExample
' 導入命名空間
Imports System.IO
Imports System.Text
Imports System.Security.AccessControl
Module FileStreamExample
Sub Main()
Try
' Create a file and write data to it.
' Create an array of bytes.
Dim messageByte As Byte() = Encoding.ASCII.GetBytes("Here is some data.")
' Specify an access control list (ACL)
Dim fs As New FileSecurity()
fs.AddAccessRule(New FileSystemAccessRule("DOMAINNAME\AccountName", FileSystemRights.ReadData, AccessControlType.Allow))
' Create a file using the FileStream class.
Dim fWrite As New FileStream("test.txt", FileMode.Create, FileSystemRights.Modify, FileShare.None, 8, FileOptions.None, fs)
' Write the number of bytes to the file.
fWrite.WriteByte(System.Convert.ToByte(messageByte.Length))
' Write the bytes to the file.
fWrite.Write(messageByte, 0, messageByte.Length)
' Close the stream.
fWrite.Close()
' Open a file and read the number of bytes.
Dim fRead As New FileStream("test.txt", FileMode.Open)
' The first byte is the string length.
Dim length As Integer = Fix(fRead.ReadByte())
' Create a new byte array for the data.
Dim readBytes(length) As Byte
' Read the data from the file.
fRead.Read(readBytes, 0, readBytes.Length)
' Close the stream.
fRead.Close()
' Display the data.
Console.WriteLine(Encoding.ASCII.GetString(readBytes))
Console.WriteLine("Done writing and reading data.")
Catch e As Exception
Console.WriteLine(e)
End Try
Console.ReadLine()
End Sub
End Module
示例2: FStream
' 導入命名空間
Imports System.IO
Imports System.Threading
Class FStream
Shared Sub Main()
' Create a synchronization object that gets
' signaled when verification is complete.
Dim manualEvent As New ManualResetEvent(False)
' Create random data to write to the file.
Dim writeArray(100000) As Byte
Dim randomGenerator As New Random()
randomGenerator.NextBytes(writeArray)
Dim fStream As New FileStream("Test#@@#.dat", _
FileMode.Create, FileAccess.ReadWrite, _
FileShare.None, 4096, True)
' Check that the FileStream was opened asynchronously.
If fStream.IsAsync = True
Console.WriteLine("fStream was opened asynchronously.")
Else
Console.WriteLine("fStream was not opened asynchronously.")
End If
' Asynchronously write to the file.
Dim asyncResult As IAsyncResult = fStream.BeginWrite( _
writeArray, 0, writeArray.Length, _
AddressOf EndWriteCallback , _
New State(fStream, writeArray, manualEvent))
' Concurrently do other work and then wait
' for the data to be written and verified.
manualEvent.WaitOne(5000, False)
End Sub
' When BeginWrite is finished writing data to the file, the
' EndWriteCallback method is called to end the asynchronous
' write operation and then read back and verify the data.
Private Shared Sub EndWriteCallback(asyncResult As IAsyncResult)
Dim tempState As State = _
DirectCast(asyncResult.AsyncState, State)
Dim fStream As FileStream = tempState.FStream
fStream.EndWrite(asyncResult)
' Asynchronously read back the written data.
fStream.Position = 0
asyncResult = fStream.BeginRead( _
tempState.ReadArray, 0 , tempState.ReadArray.Length, _
AddressOf EndReadCallback, tempState)
' Concurrently do other work, such as
' logging the write operation.
End Sub
' When BeginRead is finished reading data from the file, the
' EndReadCallback method is called to end the asynchronous
' read operation and then verify the data.
Private Shared Sub EndReadCallback(asyncResult As IAsyncResult)
Dim tempState As State = _
DirectCast(asyncResult.AsyncState, State)
Dim readCount As Integer = _
tempState.FStream.EndRead(asyncResult)
Dim i As Integer = 0
While(i < readCount)
If(tempState.ReadArray(i) <> tempState.WriteArray(i))
Console.WriteLine("Error writing data.")
tempState.FStream.Close()
Return
End If
i += 1
End While
Console.WriteLine("The data was written to {0} and " & _
"verified.", tempState.FStream.Name)
tempState.FStream.Close()
' Signal the main thread that the verification is finished.
tempState.ManualEvent.Set()
End Sub
' Maintain state information to be passed to
' EndWriteCallback and EndReadCallback.
Private Class State
' fStreamValue is used to read and write to the file.
Dim fStreamValue As FileStream
' writeArrayValue stores data that is written to the file.
Dim writeArrayValue As Byte()
' readArrayValue stores data that is read from the file.
Dim readArrayValue As Byte()
' manualEvent signals the main thread
' when verification is complete.
Dim manualEventValue As ManualResetEvent
Sub New(aStream As FileStream, anArray As Byte(), _
manualEvent As ManualResetEvent)
fStreamValue = aStream
writeArrayValue = anArray
manualEventValue = manualEvent
readArrayValue = New Byte(anArray.Length - 1){}
End Sub
Public ReadOnly Property FStream() As FileStream
Get
Return fStreamValue
End Get
End Property
Public ReadOnly Property WriteArray() As Byte()
Get
Return writeArrayValue
End Get
End Property
Public ReadOnly Property ReadArray() As Byte()
Get
Return readArrayValue
End Get
End Property
Public ReadOnly Property ManualEvent() As ManualResetEvent
Get
Return manualEventValue
End Get
End Property
End Class
End Class
示例3: FileStream
Dim aFileStream As New FileStream( _
"Test#@@#.dat", FileMode.OpenOrCreate, _
FileAccess.ReadWrite, FileShare.ReadWrite)
示例4: FileStreamExample
' 導入命名空間
Imports System.IO
Imports System.Text
Imports System.Security.AccessControl
Module FileStreamExample
Sub Main()
Try
' Create a file and write data to it.
' Create an array of bytes.
Dim messageByte As Byte() = Encoding.ASCII.GetBytes("Here is some data.")
' Create a file using the FileStream class.
Dim fWrite As New FileStream("test.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 8, FileOptions.None)
' Write the number of bytes to the file.
fWrite.WriteByte(System.Convert.ToByte(messageByte.Length))
' Write the bytes to the file.
fWrite.Write(messageByte, 0, messageByte.Length)
' Close the stream.
fWrite.Close()
' Open a file and read the number of bytes.
Dim fRead As New FileStream("test.txt", FileMode.Open)
' The first byte is the string length.
Dim length As Integer = Fix(fRead.ReadByte())
' Create a new byte array for the data.
Dim readBytes(length) As Byte
' Read the data from the file.
fRead.Read(readBytes, 0, readBytes.Length)
' Close the stream.
fRead.Close()
' Display the data.
Console.WriteLine(Encoding.ASCII.GetString(readBytes))
Console.WriteLine("Done writing and reading data.")
Catch e As Exception
Console.WriteLine(e)
End Try
Console.ReadLine()
End Sub
End Module
示例5: 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