Write and read a file in Visual Basic 2005?

Hello Microsoft.

I need to know how to read or write from / to a file with Visual Basic 2005. This is quite hard for me, when I'm used to Visual Basic 6.




Answer this question

Write and read a file in Visual Basic 2005?

  • toypaj

    Since I'm lazy, I'm not even gonna look at that dude. Can you like shorten the lines a little I'm not gonna try to understand the meaning of all that ;)



  • JenLS

    Imports System.io

    Public Class Form1

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            Dim io As New IONOW
            Dim ios As New IONOW.iosb

            IO.Openwrite("test.dat")

            IO.writedata("Hello World")

            IO.closewrite()

            IO.openread("test.dat")

            ios = io.ReadFile(ios)

            IO.closeread()

            MsgBox(ios.Buf, MsgBoxStyle.OkOnly, "here you are")


        End Sub

        Private Sub cbGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbGo.Click

        End Sub


        Private Sub cbExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbExit.Click
            End
        End Sub

        Public Class IONOW
            Public Const cEOF As Short = 42
            Public Structure iosb
                Public Status As Boolean
                Public Astat As Short
                Public ByteCount As Short
                Public Buf As String
            End Structure

            Dim sw As StreamWriter
            Dim sr As StreamReader

            Public Sub WriteData(ByVal line As String)
                Try
                    sw.WriteLine(line)
                Catch e As Exception
                    MsgBox("Unable to write output file", MsgBoxStyle.Critical, e.Message)
                End Try

            End Sub 'Log ' The using closes the stream

            Public Function ReadFile(ByVal IOSB As iosb, Optional ByVal bSkip As Boolean = False) As iosb

                ReadFile = Nothing

                ReadFile.Status = False
                Try
                    If Not sr.EndOfStream Then
                        If Not bSkip Then
                            ReadFile.Buf = sr.ReadLine()
                            ReadFile.ByteCount = ReadFile.Buf.Length
                            ReadFile.Status = True
                        Else
                            ReadFile.ByteCount = 0
                            ReadFile.Status = 0
                        End If
                    Else
                        ReadFile.Astat = cEOF
                        ReadFile.Status = False
                        ReadFile.Buf = ""
                    End If
                Catch e As SystemException
                    MsgBox("Unable to read input file", MsgBoxStyle.Critical, e.Message)
                Finally
                End Try

                IOSB = ReadFile

            End Function

            Public Function Openread(ByVal filespec As String) As StreamReader

                Openread = Nothing
                Try
                    Openread = New StreamReader(filespec)
                Catch e As Exception
                    MsgBox("Unable to open output file: " & filespec, MsgBoxStyle.Information, "Output File Open Error")
                End Try

                If Openread IsNot Nothing Then sr = Openread

            End Function

            Public Function OpenWrite(ByVal Filespec As String) As StreamWriter

                OpenWrite = Nothing

                Try
                    OpenWrite = New StreamWriter(Filespec)
                Catch e As Exception
                    MsgBox("Unable to open output file: " & Filespec, MsgBoxStyle.Information, "Output File Open Error")
                End Try

                If OpenWrite IsNot Nothing Then sw = OpenWrite

            End Function

            Public Sub CloseRead()

                sr.Dispose()
            End Sub

            Public Sub CloseWrite()
                sw.Dispose()
            End Sub


        End Class


    End Class



  • bobberino1

    Make use of code snippets in VB express, right click in the code where you want to load the file and select

    insert snippet

    then select Processing Drives, Folders, and Files

    then select the appropriate code for your task

    at the least it will give you an idea of how to go about doing it.  But it realy depends on what type or types of files your dealing with.

    Download the snippet explorer to add and edit your own snippets at http://msdn.microsoft.com/vbasic/downloads/tools/snippeteditor/

    ReneeC:

       That seems verry advanced  ; but it may come in handy some day. (Cut and paste to the snippet editor)



  • KCSmith

     

    Japreja,

    I really don't think it's advanced. It's just average vb coding.

    What does it give you good clean text I/O with exception handling. I'd use it as a class and not a snippet.

    Please inform Mathy that he's a dude and I am a woman.

     



  • Write and read a file in Visual Basic 2005?