Saving Data

Hi, I am new to vb.net.

I have a form with some fields on it.  I want to be able to save the data on the form and have it retrieved next time the program runs.  This is not a database application and there will not be more than one record.  I basically just want to save some user settings.  What is easiest way of doing this?
LVL 5
jet46Asked:
Who is Participating?
 
gbzhhuConnect With a Mentor Commented:
The best way I know is to create a class that has the same fields as your form,  Say class Data add all the fields that you need.  Also add public property for each private field (so that you can access the data later on in the form)

Make sure these directives are included at the top of your class

using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

Then add these methods to the class

    public void SerializeNow()
    {
        File f=new File(@"c:\temp.dat");
        Stream s=f.Open(FileMode.Create);
        BinaryFormatter b=new BinaryFormatter();
        b.Serialize(s,this);
        s.Close();
    }

    public void DeSerializeNow()
    {
        File f=new File(@"c:\temp.dat");
        Stream s=f.Open(FileMode.Open);
        BinaryFormatter b=new BinaryFormatter();
        this=(yourClassName)b.Deserialize(s);
        s.Close();
    }

Where yourClassName is the name you gave your class.

When you are ready to serialize your data do this (in the form)

yourClassName cts = new yourClassName();
cts.SerializeNow();

File will be saved in the path given
....

When you want to re restore user saved data do this (in the form)

yourClassName cts = new yourClassName ();
cts.DeSerializeNow();

Then you can access the properties of the class

MessageBox.Show(cts.SomeProperty.ToString());

Code is typed in NotePad so may have some syntax errors

If you want to save XML instead it is a similar process, let me know and i will post code here

Cheers





0
 
tolgaongCommented:
It will be more easy to use a text file I think. Or XML if you like

I personally do not recommend registry for this purpose because, some users do not have write access to the registry -for security reasons- and it is hard to back up that data.
0
 
jet46Author Commented:
How is this done?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
jet46Author Commented:
Sorry it took so long to get back.  What language is that?  How would this be done in VB?
0
 
gbzhhuCommented:
That was C#, can you convert to VB.net?  if not let me know and I will do it but I am busy right now maybe later on today (maybe my lunch time break)
0
 
gbzhhuCommented:
The only person who provided a solution is me and user didn't continue participating, therefore I believe I deserve the points
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.