We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,605 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Opening a file in Visual Basic 2010

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?

5
Contributors
10
Replies
3 Weeks
Discussion Span
1 Year Ago
Last Updated
14
Views
ng5
Light Poster
42 posts since Dec 2011
Reputation Points: 19
Solved Threads: 0
Skill Endorsements: 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
Netcode
Veteran Poster
1,037 posts since Jun 2009
Reputation Points: 43
Solved Threads: 70
Skill Endorsements: 0

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

ng5
Light Poster
42 posts since Dec 2011
Reputation Points: 19
Solved Threads: 0
Skill Endorsements: 0

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

Netcode
Veteran Poster
1,037 posts since Jun 2009
Reputation Points: 43
Solved Threads: 70
Skill Endorsements: 0

yes

ng5
Light Poster
42 posts since Dec 2011
Reputation Points: 19
Solved Threads: 0
Skill Endorsements: 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.

ng5
Light Poster
42 posts since Dec 2011
Reputation Points: 19
Solved Threads: 0
Skill Endorsements: 0

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

Begginnerdev
Practically a Posting Shark
892 posts since Apr 2010
Reputation Points: 198
Solved Threads: 149
Skill Endorsements: 9

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

ng5
Light Poster
42 posts since Dec 2011
Reputation Points: 19
Solved Threads: 0
Skill Endorsements: 0

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

ng5
Light Poster
42 posts since Dec 2011
Reputation Points: 19
Solved Threads: 0
Skill Endorsements: 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...

ng5
Light Poster
42 posts since Dec 2011
Reputation Points: 19
Solved Threads: 0
Skill Endorsements: 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
codeorder
Postaholic
2,124 posts since Aug 2010
Reputation Points: 256
Solved Threads: 387
Skill Endorsements: 8

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.3316 seconds using 2.69MB