Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] SaveFile / LoadFile

Started by Vaitork Feb 29, 2008 at 9:53 AM 6 replies 1.1k views
Original Post
Vaitork
Vaitork
Hi everyone! I'm working on a SaveLoading feature for a program, and a friend recommended me to come here and ask about it. I coded the SaveFile feature already, which seems to be working just fine. Allow me to explain: I have a MDIForm with this simple code for the Save button: Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click GuardarArchivo() End Sub So, no mystery there. I will work on the SavingDialog sometime in the future. GuardarArchivo() is a Sub I created in a different Module, which does the saving using a BinaryFormatter. I'm using that because it's the easiest one to use and because I don't really care about being able to see the contents of the saved file. The code is as follows: Public Sub GuardarArchivo() Dim BinaryFormatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter() Dim MemoryStream As New IO.MemoryStream Dim GuardarClassAbrirGuardar As New ClassAbrirGuardar GuardarClassAbrirGuardar.GuardarTextBox1 = FormInicio.TextBox1.Text GuardarClassAbrirGuardar.GuardarTextBox2 = FormInicio.TextBox2.Text GuardarClassAbrirGuardar.GuardarTextBox3 = FormInicio.TextBox3.Text BinaryFormatter.Serialize(MemoryStream, GuardarClassAbrirGuardar) My.Computer.FileSystem.WriteAllText("C:\Test.txt", Convert.ToBase64String(MemoryStream.ToArray()), False) End Sub ClassAbrirGuardar is a serializable Class where I'm creating the variables: Public Class ClassAbrirGuardar Public GuardarTextBoxTítulo As String Public GuardarTextBoxPrint As String Public GuardarTextBoxPlot As String End Class So, alright, everything works here. I'm creating my Text.txt with a lot of serialized glibberish. Now the problem is... How to get it back again? I read somewhere about using something like this: Private Function Load() Dim FilIndhold As String = My.Computer.FileSystem.ReadAllText("C:\Test.txt") Dim BinaryFormatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter() Dim MemoryStream As New IO.MemoryStream(Convert.FromBase64String(FilIndhold)) Return (DirectCast(BinaryFormatter.Deserialize(MemoryStream), ClassAbrirGuardar)) End Function However, I'm getting nothing. How can I load everything back to where it was from a saved file? Any clues?
Machaira
Machaira
Umm, why are you serializing to a MemoryStream instead of the file. That's an extra step.
Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development
Vaitork
Vaitork
To be quite honest with you, I have no idea. I was given those directions, I followed them and after a bit of work, it worked :) it ain't really broken, and I'm more concerned about the Loading part really.

If you have a suggestion though, I'm all ears.
Machaira
Machaira
If you fix both the load and save to go directly to the file it should work.
Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development
Vaitork
Vaitork
Well, that's precisely the reason I'm posting here. Code-wise, I have no idea on how to do that.

As far as I can tell, I deserialized the contents of the file. Now, on how to get that back to the controls on my program, I fail miserably.

You understand my predicament, do you? I'm no expert at this.
benryves
benryves
Part of the problem could be that you're going to and from a text file (via text-handling methods) when you're dealing with binary data.

I've never tried to serialise an entire form, nor used the binary serialisation stuff myself, but I suspect it should look something like this:

' Serialising "ObjectToSerialise" to file "SavedFile.bin"Using SaveStream As Stream = File.Create("SavedFile.bin")	Dim Formatter As New BinaryFormatter()	Formatter.Serialize(SaveStream, ObjectToSerialise)End Using' Deserialising "SavedFile.bin" back into "ObjectToDeserialise"Dim ObjectToDeserialiseUsing SaveStream As Stream = File.OpenRead("SavedFile.bin")	Dim Formatter As New BinaryFormatter()	ObjectToDeserialise = Formatter.Deserialize(SaveStream)End Using


Edit: Replaced File.OpenWrite with File.Create

[Edited by - benryves on February 29, 2008 7:54:36 PM]
[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]
Vaitork
Vaitork
I guess I'll have to try that next time I check that part of the code. Thanks for the help.
Machaira
Machaira
You could also use the XMLFormatter if you want the file to be readable/editable.
Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.