|
-
Aug 13th, 2011, 03:07 PM
#1
Thread Starter
Lively Member
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
-
Aug 13th, 2011, 04:43 PM
#2
Re: file writer
do you want 1 file for each element of your items array, or 1 file containing the whole array?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 14th, 2011, 01:36 AM
#3
Thread Starter
Lively Member
Re: file writer
 Originally Posted by .paul.
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
-
Aug 14th, 2011, 01:43 AM
#4
Re: file writer
try this:
vb Code:
For ItemNum = 0 To Item.getupperbound(0)
Dim Filename As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "\Data\Items\" & (ItemNum + 1).ToString & ".item")
IO.File.WriteAllText(Filename, item(ItemNum))
Next
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 14th, 2011, 03:46 AM
#5
Thread Starter
Lively Member
Re: file writer
 Originally Posted by .paul.
try this:
vb Code:
For ItemNum = 0 To Item.getupperbound(0)
Dim Filename As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "\Data\Items\" & (ItemNum + 1).ToString & ".item")
IO.File.WriteAllText(Filename, item(ItemNum))
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
-
Aug 14th, 2011, 05:07 AM
#6
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 14th, 2011, 05:31 AM
#7
Thread Starter
Lively Member
Re: file writer
 Originally Posted by .paul.
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
-
Aug 14th, 2011, 06:45 AM
#8
Re: file writer
vb Code:
Item(ItemNum).Name = IO.File.ReadAllText(Filename)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|