VB.Net/File Directory/Binary File Write — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:45, 26 мая 2010

Creating a random file

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
      " record for writing to disk
      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


Save Structure to a Binary File

Imports System
Imports System.IO
Imports System.Windows.Forms
Public Class MainClass
   Shared Sub Main()
        Dim emp As New Employee
        Dim file_num As Integer = FreeFile()
        FileOpen(file_num, "MYFILE.DAT", OpenMode.Random, _
             OpenAccess.ReadWrite, OpenShare.Shared, _
             Len(emp))
        FilePut(file_num, New Employee(1, "A", "A"))
        FilePut(file_num, New Employee(2, "B", "B"))
        FilePut(file_num, New Employee(3, "C", "C"))
        FilePut(file_num, New Employee(4, "D", "D"))
        FilePut(file_num, New Employee(5, "E", "E"))
        FilePut(file_num, New Employee(6, "F", "F"))

        " Close the file.
        FileClose(file_num)
   End Sub 
End Class
    Public Structure Employee
        Public ID As Integer
        <VBFixedString(15)> Public FirstName As String
        <VBFixedString(15)> Public LastName As String
        Public Sub New(ByVal new_id As Integer, ByVal first_name As String, _
         ByVal last_name As String)
            ID = new_id
            FirstName = first_name
            LastName = last_name
        End Sub
        Public Overrides Function ToString() As String
            Return ID & ": " & FirstName & " " & LastName
        End Function
    End Structure


Use BinaryWriter to store information into a binary file

Imports System
Imports System.IO
Public Class MainClass
  Shared Sub Main()
    Dim aFileStream As FileStream
    Try
      aFileStream = New FileStream("test.txt", FileMode.OpenOrCreate,FileAccess.Write)
      Dim myBinaryWriter As New BinaryWriter(aFileStream)
      myBinaryWriter.Write("Hello world")
      myBinaryWriter.Write(1)
    Catch e As Exception
      Console.WriteLine(e.StackTrace)
    Finally
      If Not (aFileStream Is Nothing) Then aFileStream.Close()
    End Try
  End Sub
  
End Class