mike-wigan Posted October 6, 2005 Posted October 6, 2005 (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 October 6, 2005 by mike-wigan Quote
Administrators PlausiblyDamp Posted October 6, 2005 Administrators Posted October 6, 2005 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? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mike-wigan Posted October 6, 2005 Author Posted October 6, 2005 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 Quote
Administrators PlausiblyDamp Posted October 6, 2005 Administrators Posted October 6, 2005 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 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.