1,105,380 Community Members

Opening a file in Visual Basic 2010

Member Avatar
ng5
Light Poster
42 posts since Dec 2011
Reputation Points: 9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
Netcode
Veteran Poster
1,049 posts since Jun 2009
Reputation Points: 33 [?]
Q&As Helped to Solve: 86 [?]
Skill Endorsements: 8 [?]
 
0
 

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
Member Avatar
ng5
Light Poster
42 posts since Dec 2011
Reputation Points: 9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Netcode
Veteran Poster
1,049 posts since Jun 2009
Reputation Points: 33 [?]
Q&As Helped to Solve: 86 [?]
Skill Endorsements: 8 [?]
 
-1
 

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

Member Avatar
ng5
Light Poster
42 posts since Dec 2011
Reputation Points: 9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

yes

Member Avatar
ng5
Light Poster
42 posts since Dec 2011
Reputation Points: 9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Begginnerdev
Veteran Poster
1,142 posts since Apr 2010
Reputation Points: 251 [?]
Q&As Helped to Solve: 191 [?]
Skill Endorsements: 15 [?]
 
0
 

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

Member Avatar
ng5
Light Poster
42 posts since Dec 2011
Reputation Points: 9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
ng5
Light Poster
42 posts since Dec 2011
Reputation Points: 9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
ng5
Light Poster
42 posts since Dec 2011
Reputation Points: 9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
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...

Member Avatar
codeorder
Postaholic
2,027 posts since Aug 2010
Reputation Points: 197 [?]
Q&As Helped to Solve: 390 [?]
Skill Endorsements: 10 [?]
 
0
 

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
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: