【问题标题】:Stream Writer File being used exceptionStream Writer File 正在使用异常
【发布时间】:2018-04-03 03:25:18
【问题描述】:

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

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,程序会说正在使用该文件。我没有在任何其他过程中使用这个文件,所以我猜它来自我的游戏。会是什么?谢谢。

【问题讨论】:

  • 在创建 StreamWriter 之前创建 FileStream 是否有特定原因?同样对于一次性对象,通常最好使用 using 块,这样即使遇到异常也会自动进行清理。
  • 我现在删除了它,但仍然是同样的错误。现在在 subs 中创建了 sw 和 sr 但仍然是相同的错误
  • 在循环之后直接将新的 StreamReader 分配给 sr for data.mf 之前,您没有关闭第一个 StreamReader。
  • 我对这件事并没有真正的经验。我昨天刚开始在硬盘中保存文件,当我添加多个文件源时,我不知道该怎么做。谢谢
  • 那么成功了吗?

标签: vb.net save streamreader streamwriter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 2012-04-07
    • 1970-01-01
    • 2011-04-23
    • 2019-02-15
    相关资源
    最近更新 更多