[RESOLVED] Writing And Reading File help!
Hello all,
I am still very new at this and have run into a second problem. I have created a small game for my son and after each game it tells him his score..........all I want is to write the highscore to a file and have it open each time he begins a game......so he can see what his last score was. I dabbled with all this Io.file stuff and I just can not seem to get it to write to the file.....also how do I work with app path in this case? I mean when the game is done and installed the current path of the save text would change....please help if you can and please keep it simple as I am well a newb!
Re: Writing And Reading File help!
Hi
Use this two subs to save and load the score from the application directory, my advice is that you keep reading about the StreamWriter and StreamReader and get better idea of how they work.
Code:
Private Sub SaveScore(ByVal score)
Dim sw As New StreamWriter(System.AppDomain.CurrentDomain.BaseDirectory() & "text.txt")
sw.WriteLine(score)
sw.Close()
End Sub
Private Sub LoadScore()
Dim score As String
Dim sr As New StreamReader(System.AppDomain.CurrentDomain.BaseDirectory() & "text.txt")
score = sr.ReadLine()
MsgBox(score & System.AppDomain.CurrentDomain.BaseDirectory().ToString)
sr.Close()
End Sub
Re: Writing And Reading File help!
If all you want is to save the user score than you can save it in the application settings. Create a user setting lets say “UserScore”. Go to project properties and than click the Settings tab. There you can add the field by entering the name and the type. Than you can set or get it like this:
vb Code:
'Set user score to 1.
My.Settings.UserScore = 1
'Get user score.
Dim score As Integer = My.Settings.UserScore
Re: Writing And Reading File help!
Then to display it with VBDT's method just do somehting like.
VB.NET Code:
MsgBox("Current Highscore: " & My.Settings.UserScore, MsgBoxStyle.Information, "Highscore")
Re: Writing And Reading File help!
If your thread is resolved mark it resolved by clicking here.
Re: Writing And Reading File help!
This is a short example of how to read/write to a text file. I would suggest you read the documentation available at msdn, for more information.
Anyhow, I would use VBDT's way, it's simpler.
vb.net Code:
Private Sub Form1_FormClosing _
(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles Me.FormClosing
'To write to the file you can do this
Dim _newscore As String = TextBox1.Text
IO.File.WriteAllText("filepath", "Your last score was" & TextBox1.Text)
'Another way of accomplishing the same thing is
Using writer As New IO.StreamWriter("filepath")
writer.WriteLine("Your last score was" & TextBox1.Text)
End Using
End Sub
Private Sub Form1_Load _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
'Note that ReadAllText returns a string with all the lines of
'a text file, so I'm assuming the file has one line...
Dim score As String = IO.File.ReadAllText("filepath")
'The other thing I'm assuming is that the file says something like
'Your last score was 1000! Nice!
MsgBox(score, MsgBoxStyle.Exclamation, "Highscore")
'Another way to read a text file is this
Using reader As New IO.StreamReader("filepath")
Dim _score As String = reader.ReadToEnd
TextBox2.Text = _score
End Using
End Sub
Re: Writing And Reading File help!
Thanks all very much! Not sure what I would do without this forum!
Thanks again!