Search code examples
vb.netsavestreamreaderstreamwriter

Stream Writer File being used exception


I am making my game and i just added saving the world. This is my code:

Imports Game_Functions_VB.NET.Functions.VBFuncs
Imports System.IO
Namespace SaveMap
Public Class SaveMap
    Public Shared CurrentMap As Byte
    Public Shared Worlds(254) As String
    Public Shared MaxWorlds As Byte = 0
    Public Shared LMap As Boolean
    Public Shared SMap As Boolean
    Shared sw As StreamWriter
    Shared sr As StreamReader

    Shared Sub SaveMap(MapName As String)

        '   Try

        Directory.CreateDirectory("C:\Better Survival\Saves\" & MapName)
        Dim newfile As FileStream = File.Create("C:\Better Survival\Saves\" & MapName & "\" & MapName & ".mf")
        newfile.Close()
        sw = New StreamWriter("C:\Better Survival\Saves\" & MapName & "\" & MapName & ".mf")
        Dim strline As String = ""
        For Y As Short = 0 To 1024
            For X As Short = 0 To 1024
                strline = strline & Map(X, Y, 0) & ","
            Next
            sw.WriteLine(strline)
            strline = ""
        Next

        sw = New StreamWriter("C:\Better Survival\Saves\" & MapName & "\" & "data.mf")
        sw.WriteLine(LocX)
        sw.WriteLine(LocY)
        sw.Flush()
        sw.Close()
        sw.Dispose()
        SMap = True


        '  Catch ex As Exception
        '  MsgBox("Error saving the map.", MsgBoxStyle.Critical)
        SMap = False
        ' End Try

    End Sub

    Shared Sub LoadMap(MapName As String)
        Dim X As Short
        Dim Y As Short
        Dim gotit As Boolean

        If IO.File.Exists("C:\Better Survival\Saves\" & MapName & "\" & MapName & ".mf") Then
            Try
                sr = New StreamReader("C:\Better Survival\Saves\" & MapName & "\" & MapName & ".mf")

                gotit = True
            Catch ex As Exception
                gotit = False
            End Try
        End If
        If gotit = True Then

            Try
                Dim strline As String = ""
                Do Until sr.EndOfStream
                    strline = sr.ReadLine
                    strline = strline.Replace(strline.LastIndexOf(","), "")
                    For Each item As String In Split(strline, ",", -1)
                        If item = "" Then
                            item = 0
                        End If

                        If X <= 1024 Then
                            Map(X, Y, 0) = Int(item)
                        End If
                        X = X + 1
                    Next
                    X = 0
                    Y = Y + 1
                Loop
                sr = New StreamReader("C:\Better Survival\Saves\" & MapName & "\" & "data.mf")
                Dim line As Byte = 0
                Do Until sr.EndOfStream
                    Select Case line
                        Case 0
                            LocX = sr.ReadLine
                        Case 1
                            LocY = sr.ReadLine
                    End Select
                    line += 1
                Loop
                LMap = True
                sr.Close()
                sr.Dispose()

            Catch ex As Exception
                MsgBox("Error loading the map.", MsgBoxStyle.Critical)
                LMap = False
            End Try
        Else
            MsgBox("The map does not exist", MsgBoxStyle.Critical)
        End If


    End Sub

    Shared Sub GetMaps()
        MaxWorlds = 0
        For t As Byte = 0 To 254
            Worlds(t) = ""
        Next

        Dim i As Byte = 0
        For Each File As String In My.Computer.FileSystem.GetDirectories("C:\Better Survival\Saves")
            Worlds(i) = RewritePath(File)
            i += 1
            MaxWorlds += 1
        Next
    End Sub

    Shared Sub ChangeMap()
        If CurrentMap < MaxWorlds - 1 Then
            CurrentMap = CurrentMap + 1
        End If
    End Sub

    Shared Sub ChangeMapLess()
        If CurrentMap > 0 Then
            CurrentMap = CurrentMap - 1
        End If
    End Sub

    Shared Function RewritePath(file As String) As String
        Dim line As String = ""
        If file <> "" Then

            For i As Byte = 25 To file.Length - 1
                line = line & file.Chars(i)
            Next
        End If
        Return line
    End Function

    Shared Sub DeleteWorld()
        Try
            Kill("C:\Better Survival\Saves\" & Worlds(CurrentMap) & "\" & Worlds(CurrentMap) & ".mf")
            Kill("C:\Better Survival\Saves\" & Worlds(CurrentMap) & "\" & "data.mf")
            Directory.Delete("C:\Better Survival\Saves\" & Worlds(CurrentMap))
            GetMaps()
        Catch ex As Exception
            MsgBox("Error deleting the world", MsgBoxStyle.Critical)
        End Try
    End Sub

    Shared Function CheckBeforeDeleting() As Boolean
        If MaxWorlds <> 0 Then
            Return True
        Else
            MsgBox("You have no worlds. How is it possible for me to delete one?", MsgBoxStyle.Exclamation)
            Return False
        End If
    End Function

    Shared Function CheckIfExists(MapName As String) As Boolean
        If IO.Directory.Exists("C:\Better Survival\Saves\" & Worlds(CurrentMap)) Then
            Return True
        End If
        Return False
    End Function

    Shared Function CheckIfMapExists(mapname As String) As Boolean
        If Directory.Exists(mapname) Then
            Return True
        End If

        Return False
    End Function
End Class
End Namespace

The sub cycling happens this way. While the game is in the world choosing menu, it executes constantly the GatMaps(). Then a world is loaded and it is shown in my screen. If i click saveworld when i dont use the "Try" the program says that that file is being used. I wasn't using this file in any other process so i gess it's from my game. What can it be? Thanks.


Solution

  • you are not closing your first StreamReader before you are assigning a new StreamReader to sr for data.mf directly after the loop.