Stream Writer 文件被使用异常

Stream Writer File being used exception

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

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()。然后加载了一个世界并显示在我的屏幕上。如果我在不使用 "Try" 时单击 saveworld,程序会提示正在使用该文件。我没有在任何其他进程中使用这个文件,所以我猜它来自我的游戏。它可以是什么?谢谢

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