I've searched up different solutions to help me debug my program but nothing is working. I'm creating an item generator that generates the items and the stats and then creates and writes to a .txt file. The only problem is that it isn't writing to the text file and i can't figure out why. Here's the code that creates and writes to the file:
'Creates item text file
TextName = "Item" & ItemCount & "." & ItemType & ".Level" & Level & "." & itemClass & "." & Rarity(0)
Dim path As String = "C:\Users\ryanl3\Desktop\My Stuff\Realms\Items\" & TextName & ".txt"
'Appends the stats to text file
Dim fw As System.IO.StreamWriter
fw = File.CreateText(path)
fw.WriteLine("Name: " & itemName)
fw.WriteLine("Type: " & ItemType)
fw.WriteLine("Damage: " & itemDamage)
fw.WriteLine("Class: " & itemClass)
fw.WriteLine("Rarity: " & Rarity(0))
I know the stats generation code is working because some of it is stored in the text file name. This block of code is repeated throughout the entire source at least twenty times so if i can get this first one fixed i can apply it to the rest.
As explained in comments, a stream needs to be closed and disposed to have its internal buffer flushed to disk. You can hope that the StreamWriter goes out of scope and then the garbage collector will close it for you but in a solid app you should take care of this yourself.
The using statement is perfect for this kind of situations
Using fw = File.CreateText(path)
fw.WriteLine("Name: " & itemName)
fw.WriteLine("Type: " & ItemType)
fw.WriteLine("Damage: " & itemDamage)
fw.WriteLine("Class: " & itemClass)
fw.WriteLine("Rarity: " & Rarity(0))
End Using
When the code reaches the End Using the compiler add the required code to Dispose the disposable object created inside the using block at the start of this code. In this way you free the resource (the file handle) and everything should work smoothly.