Saving and loading small amounts of user generated data

P

Peter Webb

I have written a specialised drawing application. I want the user to be able
to load or save a few drawings, which I will implement as an add to
favourites/ load favourites button. The quantity of data is low - a typical
drawing could be completely recreated with a few hundred bytes of data. It
doesn't really need a delete function, at least not within the app. The
program is distributed through the standard .Net 2 facility ("OneClick" ?).

I need advice on a few levels, as to the general approach to be used.

Firstly, how to extract the data. I have a variable number of instantiations
of a "drawn object" class, and a couple of classes that relate to the
drawing environment. Obviously I could create some file structure which had
the environment variables written out first, then the number of drawn
objects classes, then the variables that I need from each drawn object
class, etc. However I can't believe that C# doesn't provide a far more
flexible structure. Would XML help? It would be quite attractive if drawing
files from older versions could be loaded from newer versions, which may
have different properties implemented - is there a method which makes this
work, at least for the scenario where fields are only added in later
versions, and never deleted?

Secondly, where to store the data. I know that I can open a file and read or
write to it, but I am hoping for some higher level construct for a few
reasons. Firstly, I currently have sample drawings derived from hard coded
data in my C# source. I would like them to be stored as favourites. I guess
I could set up the save file location the first time the program is run (by
checking if the file exists and creating if it doesn't), but in general I
would like to avoid the complexity of the program or the user setting a save
file location. (And where do I store the file path itself - what am I
supposed to do?). Does the .NET 2 environment provide a scratchpad directory
that I can directly read and write without specifying a file path? Could I
store my data in the Registry? Or use a cookie (though its a Forms app) or
(gasp) a .ini file? What the easiest way/place to store perhaps 1 Kbyte of
data in aggregate?

I really just need a pointer to the approach to use ...

Thanks


Peter Webb
 
A

Alberto Poblacion

Alright, first, for saving the data:

If you already have all the data for your drawing stored in a hierarchy of
classes, you can Serialize the first of the classes (which suposedly
contains references to the rest). This will result in a stream of data that
represents the contents of the whole set of classes. You can later
Deserialize that data to recover the contents of you classes. There are
several ways to Serialize data in .Net, but in your case I think that it is
best to use the XmlSerializer, since this will result in XML data, which is
what you wanted. Look for 'XmlSerilizer class' in MSDN. The main article
contains examples.

About the other question, "where to store the data":
You are looking for a "scratchpad directory" that you can write without
specifying the path. I think that the Isolated Storage of .Net will satisfy
your requirement. This is a private area automatically assigned by the
runtime, where you can create various files that will be 'private' to your
assembly. Look for "IsolatedStorageFile Class" in MSDN.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top