Stream Writer文件正在使用异常

时间:2017-10-22 13:48:55

标签: vb.net save streamreader streamwriter

我正在制作我的游戏,我只是添加了拯救世界。这是我的代码:

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

子循环以这种方式发生。当游戏在世界上选择菜单时,它会不断执行GatMaps()。然后加载一个世界,它会显示在我的屏幕上。如果我点击saveworld,当我不使用“尝试”时程序说该文件正在使用。我没有在任何其他过程中使用此文件,所以我从我的游戏中得到它。它能是什么?感谢。

1 个答案:

答案 0 :(得分:0)

在循环之后直接将新的StreamReader分配给sr for data.mf之前,您没有关闭第一个StreamReader。