Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

[PLAIN][resolved]reading a txt file in to an arrray[/PLAIN]

 

i have an array of 14 values Dim sounds(13) As String

 

and when my program closes it writes a txt file to save those values sounds.ini

 

as follows

'file Save for sounds
       Dim fs As New FileStream(Application.StartupPath & "\sounds.ini", FileMode.Create)
       Dim sw As New StreamWriter(fs)
       Dim iLp As Integer
       Try
           'Saves Sounds stored in Sounds array
           For iLp = 0 To sounds.GetUpperBound(0)
               sw.WriteLine(sounds(iLp))
           Next iLp
           'Close off text files
           sw.Close()
       Catch
           MsgBox("Could not save sound efect libary!")
       End Try

 

now my problem is i can not get the file to read when the form opens again

this is what i am using at the mo but it dont work at all.

 

       'start of reader
       Dim lines As String
       Dim mySoundsr As String = Application.StartupPath & "\save.ini"
       Dim srr As StreamReader
       Dim fs As New FileStream(Application.StartupPath & "\save.ini", FileMode.Open)
       Try
           If F Then file.Exists(mySoundsr) Then

           fs = New FileStream(mySoundsr, FileMode.Open)
           srr = New StreamReader(fs)

           lines = srr.ReadToEnd
           srr.Close()

           sounds = lines.Split(Environment.NewLine)

           Else
           MessageBox.Show("File not found")
           End If

       Catch ex As Exception
           MessageBox.Show("Error reading file: " & ex.Message)
       End Try
       srr.Close()
       'end of reader

 

help please i have been at this al yesterday with no luck

Edited by mike-wigan
Posted
Are you getting any errors when trying to read or is it just failing to populate the array?

If you step through the code in the debugger is there a particular line where it starts to do unexpected things?

 

i just need to scrap the reader code as its been messed about with that much im getting all sorts of errors

  • Administrators
Posted

Not tested it but try something like the following

Dim lines As String
Dim sounds() As String
Dim mySoundsr As String = Application.StartupPath & "\save.ini"
Dim srr As StreamReader

   Try
       If File.Exists(mySoundsr) Then
           srr = New StreamReader(mySoundsr)
           lines = srr.ReadToEnd
           sounds = lines.Split(Environment.NewLine)
       Else
           MessageBox.Show("File not found")
       End If
   Catch ex As Exception
       MessageBox.Show("Error reading file: " & ex.Message)
   Finally
       If Not srr Is Nothing Then
           srr.Close()
       End If
   End Try

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...