Results 1 to 8 of 8

Thread: file writer

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    file writer

    Hello,

    I'm trying to make an editor for my game but how can I convert my old code from vb6.0 to vs 2010? I know it's all in System.IO but I can't figure out how it works

    old code:
    Code:
    Public Sub SaveItems()
    
    Dim ItemNum as long
    Dim F as string
    Dim Filename as string
    
    F = FreeFile
    Filename = App.Path & "\Data\Items\" & ItemNum & ".item"
    
    'check if the items exists
    CheckItems
    
    For ItemNum = 1 To 50
    
    Open Filename For Binary As #F
    Put #F , , Item(ItemNum)
    Close #F
    
    Next
    
    End Sub
    new code ?

    Code:
        Public Sub SaveItems()
            Dim filename As String
            Dim ItemNum as Integer
    
    For ItemNum = 0 to 50
    
            filename = "Data\Items\" & ItemNum & ".item"
    
            If Not File.Exists(filename) Then
                File.Create(filename)
            End If
    
    'here it should save the file? it should do something like this:
    ' -->
    Open Filename For Binary As #F
    Put #F , , Item(ItemNum)
    Close #F
    ' <--
    
    Next
    
    End Sub
    thanks in advaced

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: file writer

    do you want 1 file for each element of your items array, or 1 file containing the whole array?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: file writer

    Quote Originally Posted by .paul. View Post
    do you want 1 file for each element of your items array, or 1 file containing the whole array?
    1 file for each item if possoible

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: file writer

    try this:

    vb Code:
    1. For ItemNum = 0 To Item.getupperbound(0)
    2.     Dim Filename As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "\Data\Items\" & (ItemNum + 1).ToString & ".item")
    3.     IO.File.WriteAllText(Filename, item(ItemNum))
    4. Next

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: file writer

    Quote Originally Posted by .paul. View Post
    try this:

    vb Code:
    1. For ItemNum = 0 To Item.getupperbound(0)
    2.     Dim Filename As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "\Data\Items\" & (ItemNum + 1).ToString & ".item")
    3.     IO.File.WriteAllText(Filename, item(ItemNum))
    4. Next
    thanks, works fine! I've changed the code a bit but now it errors again, it says it can't access the file because it's being used.

    Code:
        Public Sub SaveItem(ByVal Itemnum As Integer)
    
            Dim FileName As String
    
            FileName = "Data\Items\" & Itemnum & ".item"
    
            If Itemnum < 0 Or Itemnum > max_items Then Exit Sub
    
            Try
    
                If Not File.Exists(FileName) Then
                    File.Create(FileName)
                    'Here it should close the just created file 
                End If
    
                IO.File.WriteAllText(FileName, Item(Itemnum).Name)
    
            Catch ex As Exception
    
                MsgBox("could not save item " & Itemnum & " because: " & Err.Description)
    
            End Try
    
        End Sub
    I think it is because if the file not exists it will create the file, but once created, the sub is not closing the file anymore. So how do I close my just created file? File.Close or something isnt in the list

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: file writer

    there's no need to check if the file exists + create if not.
    writealltext will create a new file or overwrite an existing file

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: file writer

    Quote Originally Posted by .paul. View Post
    there's no need to check if the file exists + create if not.
    writealltext will create a new file or overwrite an existing file
    ah, I see. thanks.

    next problem: now it needs to get the name of the item when you load the form so you know what item you're editing. But it errors at the red line. "Value of type 'String' cannot be converted to 'System.Text.Encoding'."

    Code:
        Public Sub LoadItem(ByVal ItemNum As Integer)
    
            Dim Filename As String
            Filename = "Data\Items\" & ItemNum & ".item"
    
            With frmItemEditor
    
                Try
    
                    'Prevents error if the file does not exist
                    CheckItem(ItemNum)
    
                    Item(ItemNum).Name = IO.File.ReadAllText(Filename, Item(ItemNum).Name)
    
    
                    If ItemNum < 10 Then
    
                        .lstIndex.Items.Add("00" & ItemNum & ": " & Item(ItemNum).Name)
    
                    ElseIf ItemNum < 100 Then
    
                        .lstIndex.Items.Add("0" & ItemNum & ": " & Item(ItemNum).Name)
    
                    ElseIf ItemNum < 1000 Then
    
                        .lstIndex.Items.Add(ItemNum & ": " & Item(ItemNum).Name)
    
                    End If
    
                Catch ex As Exception
    
                    MsgBox("Could not load item " & ItemNum & " because: " & Err.Description)
    
                End Try
    
            End With
    
        End Sub
    Code:
        Public Sub CheckItem(ByVal ItemNum As Integer)
    
            Dim Filename As String = "Data\Items\" & ItemNum & ".item"
    
            If Not File.Exists(Filename) Then
                IO.File.WriteAllText(Filename, Item(ItemNum).Name)
            End If
    
        End Sub

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: file writer

    vb Code:
    1. Item(ItemNum).Name = IO.File.ReadAllText(Filename)

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