VB.Net by API/System.IO/FileStream
Содержание
FileStream.Length
Imports System
Imports System.IO
Public Class MainClass
Shared Sub Main()
Dim i As Integer
Dim theBytes(255) As Byte
For i = 0 To 255
theBytes(i) = CByte(i)
Next
Dim myFileStream As FileStream
Try
myFileStream = New FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Write)
myFileStream.Write(theBytes, 0, 256)
Finally
If Not (myFileStream Is Nothing) Then myFileStream.Close()
End Try
Dim theFile As FileStream
Try
theFile = New FileStream("test.txt",FileMode.Open, FileAccess.Read)
For i = 0 To (theFile.Length - 1)
Console.Write(theFile.ReadByte)
Next
Catch e As Exception
Throw e
Finally
If Not (theFile Is Nothing) Then theFile.Close()
End Try
End Sub
End Class
FileStream.Lock
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
FileStream.Seek
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
FileStream.SetLength
Imports System.IO
Imports System.Collections
Imports System.Windows.Forms
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Public Class MainClass
Public Shared Sub Main()
Const NUMBER_OF_RECORDS As Integer = 100
Dim blankRecord As Employee = New Employee("f","l")
Dim fileOutput As FileStream
Dim binaryOutput As BinaryWriter
Dim fileName As String = "test.dat"
Dim i As Integer
Try
fileOutput = New FileStream(fileName,FileMode.Create, FileAccess.Write)
fileOutput.SetLength(Employee.SIZE * NUMBER_OF_RECORDS)
binaryOutput = New BinaryWriter(fileOutput)
For i = 0 To NUMBER_OF_RECORDS - 1
fileOutput.Position = i * Employee.SIZE
binaryOutput.Write(blankRecord.firstName)
binaryOutput.Write(blankRecord.lastName)
Next
Console.WriteLine("File Created")
Catch fileException As IOException
Console.WriteLine("Cannot write to file")
End Try
" close FileStream
If (fileOutput Is Nothing) <> False Then
fileOutput.Close()
End If
" close BinaryWriter
If (binaryOutput Is Nothing) <> False Then
binaryOutput.Close()
End If
End Sub
End Class
<Serializable()> Public Class Employee
Public firstName, lastName As String
Shared Public SIZE As Integer = 200
Public Sub New(ByVal first As String, ByVal last As String)
firstName = first
lastName = last
End Sub
Public Overrides Function ToString() As String
Return firstName & " " & lastName
End Function
End Class
FileStream.Unlock
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
FileSystem.GetFileInfo
Public Class Tester
Public Shared Sub Main
Dim fileDetail As IO.FileInfo = My.ruputer.FileSystem.GetFileInfo("test.txt")
Console.WriteLine(fileDetail.IsReadOnly)
Console.WriteLine(CBool(fileDetail.Attributes And IO.FileAttributes.Hidden))
End Sub
End Class
New FileStream(String fileName, FileMode fm)
Imports System.IO
Module Module1
Sub Main()
Dim Stream As FileStream
Try
Stream = New FileStream("test.dat", FileMode.Create)
Catch E As Exception
Console.WriteLine("Error creating test.Dat")
Console.WriteLine("Error {0}", E.Message)
End Try
Dim BinaryStream As New BinaryWriter(Stream)
Dim Age As Integer = 21
Dim Salary As Double = 100000.0
Dim Name As String = "Joe"
Try
BinaryStream.Write(Age)
BinaryStream.Write(Salary)
BinaryStream.Write(Name)
BinaryStream.Close()
Console.WriteLine("Data written to test.Dat")
Catch E As Exception
Console.WriteLine("Error writing to test.Dat")
Console.WriteLine("Error {0}", E.Message)
End Try
"Read
Try
Stream = New FileStream("test.dat", FileMode.Open)
Catch E As Exception
Console.WriteLine("Error opening test.Dat")
Console.WriteLine("Error {0}", E.Message)
End Try
Dim BinaryStreamReader As New BinaryReader(Stream)
Try
Age = BinaryStreamReader.ReadInt32()
Salary = BinaryStreamReader.ReadDouble()
Name = BinaryStreamReader.ReadString()
BinaryStreamReader.Close()
Console.WriteLine("Age: {0}", Age)
Console.WriteLine("Salary: {0}", Salary)
Console.WriteLine("Name: {0}", Name)
Catch E As Exception
Console.WriteLine("Error reading to test.Dat")
Console.WriteLine("Error {0}", E.Message)
End Try
End Sub
End Module