hey guys i was wondering if anybody knew how to open an array into visual basic. I have it saving as a text file correctly but i have no idea on how to open it up to use it again. what i have so far is

Private Sub Hour1ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hour1ToolStripMenuItem.Click
        myLoadSubHour(1)
End Sub

Private Sub myLoadSubHour(ByVal selHourFileNumber As Integer)
        Dim myNamesFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Hour " & selHourFileNumber.ToString & ".txt"
        If File.Exists(myNamesFile) Then
            arFileLines = File.ReadAllLines(myNamesFile)
            arFileLines(AddStudent) = Display
        End If
    End Sub

Any ideas how to open it up?

Recommended Answers

All 10 Replies

This opens the file with read-only access

Imports System
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        Dim fs As FileStream

        ' Delete the file if it exists.
        If File.Exists(path) = False Then
            ' Create the file.
            fs = File.Create(path)
            Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")

            ' Add some information to the file.
            fs.Write(info, 0, info.Length)
            fs.Close()
        End If

        ' Open the stream and read it back.
        fs = File.Open(path, FileMode.Open, FileAccess.Read)
        Dim b(1024) As Byte
        Dim temp As UTF8Encoding = New UTF8Encoding(True)

        Do While fs.Read(b, 0, b.Length) > 0
            Console.WriteLine(temp.GetString(b))
        Loop

        Try
            ' Try to get another handle to the same file.
            Dim fs2 As FileStream = File.Open(path, FileMode.Open)
            ' Do some task here.
            fs2.Close()
        Catch e As Exception
            Console.Write("Opening the file twice is disallowed.")
            Console.WriteLine(", as expected: {0}", e.ToString())
        End Try

        fs.Close()
    End Sub
End Class

when i try to ope the variable isnt actually loaded so the program crashes and says value cannot be null...

Did you replace the file path in line 7 with the file location on your system ?

yes

what happens is that when i open the file i need to open a variable. for the sake of not wanting to explain the entire program call this variable x (an array). i save the text in richtextbox1 and close the program. then i open the file and variable x is null (error "value cannot be null"). since x is null i cant do anything to display the info that was just opened.

Are you creating X dynamicly during run time or is X a public variable that is created on form load?

"x" is a public variable defined within a prompt ran by a button click...

alright guys I got it. I will post the code in about 5 minutes

Imports System.IO
Public Class Form1
    Dim Strings(0 To 4) As String

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim StreamRead As StreamReader
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            TextBox1.Text = ""
            StreamRead = New StreamReader(OpenFileDialog1.FileName)
            For F = 0 To 4
                Strings(F) = StreamRead.ReadLine
                StreamRead.Read()
            Next
            StreamRead.Close()
            For F = 0 To UBound(Strings)
                TextBox1.Text = TextBox1.Text & Strings(F) & vbCrLf
            Next
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Streamwrite As StreamWriter
        Strings = TextBox1.Text.Split(vbCrLf)
        Try
            If SaveFileDialog1.ShowDialog = DialogResult.OK Then
                Streamwrite = New StreamWriter(SaveFileDialog1.FileName)
                For F = 0 To 4
                    Streamwrite.WriteLine(Strings(F))
                Next
                Streamwrite.Close()
            End If
            Exit Sub
        Catch ex As Exception
            MsgBox("Woops. There was a problem saving.")
        End Try
    End Sub
End Class

If you want something similar just change the number "4" in the line

F = 0 To 4

and make it one less than what you want. type the desired names/words into the textbox followed by "enter" and click save it like in microsoft word... and open it by clicking open...

I just overlooked the last post of this thread and wanted to provide some code.clean up assistance. Hope it helps.:)

Imports System.IO
Public Class Form1
    Private myStrings(0 To 4) As String
    Private ofd As New OpenFileDialog, sfd As New SaveFileDialog

    Private Sub _Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If ofd.ShowDialog = DialogResult.OK Then
            myStrings = File.ReadAllLines(ofd.FileName)
            TextBox1.Lines = myStrings
        End If
    End Sub

    Private Sub _Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If sfd.ShowDialog = DialogResult.OK Then
            Try
                myStrings = TextBox1.Lines
                File.WriteAllLines(sfd.FileName, myStrings)
                Exit Sub
            Catch ex As Exception
                MsgBox("Woops. There was a problem saving.")
            End Try
        End If
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Button1.Text = ".save" : Button2.Text = ".load"
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.