[.net] SaveFile / LoadFile
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:
<Serializable()> 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?
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.
If you have a suggestion though, I'm all ears.
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.
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.
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:
Edit: Replaced File.OpenWrite with File.Create
[Edited by - benryves on February 29, 2008 7:54:36 PM]
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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement