Results 1 to 7 of 7

Thread: [RESOLVED] Writing And Reading File help!

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    5

    Resolved [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!

  2. #2
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    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
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  3. #3
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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:
    1. 'Set user score to 1.
    2. My.Settings.UserScore = 1
    3.  
    4. 'Get user score.
    5. Dim score As Integer = My.Settings.UserScore

  4. #4
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Re: Writing And Reading File help!

    Then to display it with VBDT's method just do somehting like.
    VB.NET Code:
    1. MsgBox("Current Highscore: " & My.Settings.UserScore, MsgBoxStyle.Information, "Highscore")

  5. #5

  6. #6
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    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:
    1. Private Sub Form1_FormClosing _
    2.     (ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) _
    3.     Handles Me.FormClosing
    4.         'To write to the file you can do this
    5.         Dim _newscore As String = TextBox1.Text
    6.         IO.File.WriteAllText("filepath", "Your last score was" & TextBox1.Text)
    7.         'Another way of accomplishing the same thing is
    8.         Using writer As New IO.StreamWriter("filepath")
    9.             writer.WriteLine("Your last score was" & TextBox1.Text)
    10.         End Using
    11.     End Sub
    12.  
    13.     Private Sub Form1_Load _
    14.     (ByVal sender As System.Object, ByVal e As System.EventArgs) _
    15.     Handles MyBase.Load
    16.         'Note that ReadAllText returns a string with all the lines of
    17.         'a text file, so I'm assuming the file has one line...
    18.         Dim score As String = IO.File.ReadAllText("filepath")
    19.         'The other thing I'm assuming is that the file says something like
    20.         'Your last score was 1000! Nice!
    21.         MsgBox(score, MsgBoxStyle.Exclamation, "Highscore")
    22.         'Another way to read a text file is this
    23.         Using reader As New IO.StreamReader("filepath")
    24.             Dim _score As String = reader.ReadToEnd
    25.             TextBox2.Text = _score
    26.         End Using
    27.     End Sub
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    5

    Re: Writing And Reading File help!

    Thanks all very much! Not sure what I would do without this forum!

    Thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width