I want to learn how to write and read data from files using Visual Basic Studio Express 2013 for Windows Desktop.
Specifically, I have a class called Pilots. The class includes text of a first and last name, and integers representing rank, skill, missions and status.
There is an ArrayList called Squadron that contains 18 (at start) Pilots.
What I want to do is save and then load all the data in Squadron to a file. And I'm not sure how to get it to work properly. I've read numerous books and sites, but somewhere along the line, I'm just not comprehending how to get it to work correctly.
At this point, I've been able to write the data to a file.... but I'm having no luck getting it back out of the file.
To write it out... I'm using this code:
Dim currentpilot As New Pilot("John", "Doe")
' Create the BinaryWriter and use File.Open to create the file.
Using writer As BinaryWriter = New BinaryWriter(File.Open("squadron.dat", FileMode.Create))
' Write each integer.
writer.Write(SquadronList.Count)
For index = 0 To SquadronList.Count - 1
currentpilot = CType(SquadronList(index), Pilot)
writer.Write(currentpilot.pFirstName)
writer.Write(currentpilot.pLastName)
writer.Write(currentpilot.pSkill)
writer.Write(currentpilot.pRank)
writer.Write(currentpilot.pStatus)
writer.Write(currentpilot.pMissions)
writer.Write(currentpilot.pKills)
Next
End Using
The writer.Write(SquadronList.Count) line is there to record the number of actual Pilot records in the file. I don't know if that's necessary or not.
The file does write to disk and just looking at it with notepad, it does seem to have the correct data.
The problem is getting it back out. This code fails quickly...
Using reader As BinaryReader = New BinaryReader(File.OpenRead("squadron.dat"))
Dim index As Integer = reader.ReadInt32
For counter = 0 To index - 1
currentpilot.pFirstName = reader.ReadString
currentpilot.pLastName = reader.ReadString
currentpilot.pSkill = reader.ReadInt16
currentpilot.pRank = reader.ReadInt16
currentpilot.pStatus = reader.ReadInt16
currentpilot.pMissions = reader.ReadInt16
currentpilot.pKills = reader.ReadInt16
SquadronList.Add(currentpilot)
Next
End Using
So... any suggestions or guidance will be appreciated!
Related
My program is writing URL-encoded strings into a text file like this one.
topic1=1&topic2=2&topic3=3&
However, I'm trying to reverse the process by opening the same file and breaking that file up in a manner that I can retrieve into name/value-like pairs.
' Final Challenge Category
Dim sr As StreamReader = New StreamReader("./Game" + nudGameNo.Value.ToString() + "/Final/category.txt")
strLine = WebUtility.UrlDecode(sr.ReadLine)
The closest function that I can find to help is HttpUtility.ParseQueryString but I can't seem to run it in a WinForms application. (Even if I use Imports System.Web)
I've also tried to do a .Split with & being the separator, however problems start up if a particular value contains an & of it's own.
Is it possible to break this form of string up?
I upgraded to Visual Studio 2015 from 2012, and it royally screwed me over. I can no longer import WAV files into my project's resources without it turning them into a MemoryStream, which my code won't accept. I have been searching for hours now, and I am getting really frustrated. Will someone please help me with this? I am importing the files exactly according to these instrutions: How to: Import or Export Resources
Let me know if you need pictures or other info. I am getting really desperate at this point.
I don't know exactly what experience you think you had in VS 2012 but I just tested VS 2015, 2013 and 2012 and they all worked exactly the same way. I simply opened the project properties, selected the Resources page, clicked the Add Resource drop-down, selected Add Existing File and navigated to the WAV file I wanted. The file was added as a resource and the corresponding property of My.Settings exposed that resource as type UnmanagedMemoryStream. As I said, that happened exactly the same way in all three versions. If you got something different in VS 2012 then you did something different in VS 2012. You haven't told us what you did so we can only guess.
Exactly what type of data does your code expect? Maybe that would have been good information to provide too. If it's a Byte array then you can get one from that resource Stream like so:
Dim resourceStream = My.Resources.MyWavResource
Dim length = CInt(resourceStream.Length)
Dim resourceData(length - 1) As Byte
resourceStream.Read(resourceData, 0, length)
That's exactly how you read from any Stream to a Byte array. You could, if you needed to do this more than once, put that into a method:
Private Function GetStreamData(stream As Stream) As Byte()
Dim length = CInt(stream.Length)
Dim data(length - 1) As Byte
stream.Read(data, 0, length)
Return data
End Function
You could call it like this:
Dim data As Byte()
Using resource = My.Resources.MyWavResource
data = GetStreamData(resource)
End Using
You could even write it as an extension method and then call it on the Stream itself.
The link you are using is VS 2010.
Open your Resource file. By default the Top Left menu is Strings; but there is a small drop down arrow. Click this and the fourth option is Audio. If you now click add existing file, by default it will filter for .wav files, and will add them as such.
So I'm a bit stuck with this. Basically I've got a text file in a directory and in that text file, I've added different text in each of its line. I'm trying to use this to count how many lines there are in that text file. I'm using IsolatedStorageFile to do this and this is how I'm accessing it.
So far I've got this but It's not working:
Using isoComments As IsolatedStorageFileStream = New IsolatedStorageFileStream("textfile1.txt", FileMode.Open, isoStore)
Using readerComments As StreamReader = New StreamReader(isoComments)
For Each line In readerComments.ReadLine
MessageBox.Show(line.ToString.Count)
Next
End Using
End Using
The result is not the amount of lines instead, the amount of characters in each line. This is not what I want I want the number of lines in that particular textfile. Is anyone able to help me with this.
Something like this:
Dim iLineCount As Integer
iLineCount = 0
Using isoComments As IsolatedStorageFileStream =
New IsolatedStorageFileStream("textfile1.txt", FileMode.Open, isoStore)
Using readerComments As StreamReader = New StreamReader(isoComments)
For Each line In readerComments.ReadLine
iLineCount=iLineCount+1
Next
MessageBox.Show(iLineCount)
End Using
End Using
So I managed to solve this problem. My original idea was to get the amount of times a textfile was edited and so I figured adding something in each line of that textfile and counting each line would help but i couldn't find a way to it.
What I did was I added a counter which increases by 1 everytime it saves and then when it's read +1 is added to it. That way I have an exact number of times it was edited because everytime the textfile is edited, like mentioned before, it increases like a counter by 1. so if the user edited the textfile about 12 times then there will be the number 12 in the file and that will be read when the ser decides to edit the textfile again, once he saves +1 to the 12 will be added maing it 13.
I used this method to do the increment by one:
Using isoStream As IsolatedStorageFileStream = New IsolatedStorageFileStream("textfile.text", FileMode.CreateNew, isoStore) Using writer As StreamWriter = New StreamWriter(isoStream)
Dim i As Integer
i += 1
writer.WriteLine(i)
End Using
End Using
I have written a vb program a few years ago and now as I get started with vb again i am hitting a "snag". With sequential files. I am trying to load a file in to the vb program with the file dialog box.
NOTE:I am using structures
Dim FileDialog as new openFileDialog
Dim MyStream as Stream = nothing
Dim FileLocation as string 'this is to save the file location
if( FileDialog.ShowDialog() = DialogResults.OK)Then
FS = new FileStream(FileLocation, FileMode.open, fileaccess.Read)
BF = new BinaryFromatter
While FS.Position < FS.Length
Dim temp as unit
...'Please note that this is where the file reads the structures data.It is to much code to write in.
When I run the program I can create a file and save it with the data in and I can load it with the Dialog box, The problem is when I run the program again and try to load it. It just wont run the file or load it(Remember I created the file with this program and saved)
How do I get this to work?
Make sure you have closed the file after writing and reading the data the first time, and make sure you are using the correct path (FileLocation).
Exit Visual Studio between the first and second times you run the program. If it works then, then you know you are not closing the file properly.
Set a breakpoint at the new FileStream assignment and check the value of FileLocation. Is it the same as it was when the file was written?
Check the error message, if there is one, and see if that tells you anything.
Basically I want to take My Client.
Then for example Lets say in my client I have "A = 1"
Then my 2nd file which has random data in it.
So Client= My Client
File = The File which I want in the end result
How could I Inject "A = TextBox1.Text" from Client to File.
I heard it's called "End Of File" or something like that.
Any help please?
Use System.IO.File in combination with StreamWriter.
File.CreateFile() helps you create files. Check if the following sample helps you (This is not written to solve your exact expectation, but gives you the idea of available options)
Dim fs as new FileStream("YourFile.bin", FileMode.Create)
Dim writer as New BinaryWriter(fs)
For i as Integer = 0 To 10
writer.Write(CInt(i))
Next
writer.Close()
fs.Close()
FileStream.CreateText() method helps you create a Text stream and you can use this to write your textbox content into file.