Preserve State

  • Thread starter Thread starter Martijn Mulder
  • Start date Start date
M

Martijn Mulder

In a simple desktop application, when the user starts my program again,
I want her to find things just as she left them last time. What is a
simple straightforward way to implement such persistence?
 
Martijn Mulder said:
In a simple desktop application, when the user starts my program again, I
want her to find things just as she left them last time. What is a simple
straightforward way to implement such persistence?


Look at the MSDN documentation for the
System.Configuration.ApplicationSettingsBase class and the See Also
Reference links. There are alot of links that describe how.

Also
http://msdn2.microsoft.com/en-us/library/0zszyc6e.aspx

--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?
 
In a simple desktop application, when the user starts my program
Look at the MSDN documentation for the
System.Configuration.ApplicationSettingsBase class and the See Also
Reference links. There are alot of links that describe how.

Also
http://msdn2.microsoft.com/en-us/library/0zszyc6e.aspx

When I go through the steps described in the link above and implement an
ApplicationSettingsBase-object for my Form, the system writes out a file
'user.config' somewhere deep in C:\Documents and Settings\UserName\Local
Settings\Application Data\... This file contains xml. When I
(deliberatly) corrupt this file I can no longer read from it or write to
it. The exception it raises can by caught with two try{}catch{} blocks,
one for reading it and one for writing to it. But than the whole
mechanism is vanished. I cannot read from the corrupt file, I cannot
write to the corrupt file and I cannot (untill now) replace it with a
new and valid user.config file.

Is there a safe way to start with a clean sheet, once the user.config
file is corrupted?
 
Martijn Mulder said:
When I go through the steps described in the link above and implement an
ApplicationSettingsBase-object for my Form, the system writes out a file
'user.config' somewhere deep in C:\Documents and Settings\UserName\Local
Settings\Application Data\... This file contains xml. When I (deliberatly)
corrupt this file I can no longer read from it or write to it. The
exception it raises can by caught with two try{}catch{} blocks, one for
reading it and one for writing to it. But than the whole mechanism is
vanished. I cannot read from the corrupt file, I cannot write to the
corrupt file and I cannot (untill now) replace it with a new and valid
user.config file.

Is there a safe way to start with a clean sheet, once the user.config file
is corrupted?


Well, there's a reason that the file is buried where it is. The file is
maintained by the framework...there's really no reason for you to be mucking
with it...and in fact can be located in a differnt location based on whether
it's click-once, roaming profile, even version number of the app. Let the
framework handle this file and you should be fine...

There is a Reset() function on applicationSettingsBase that you could try
calling but I think that still tries to call a Reload(), which will cause an
exception if the file is corrupt.

ANY data file that has corrupted data will cause unexpected results, whether
it's an XML file that the framework is maintaining or some binary file that
you are writing.

--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?
 
In a simple desktop application, when the user starts my program
Look at the MSDN documentation for the
System.Configuration.ApplicationSettingsBase class and the See Also
Reference links. There are alot of links that describe how.

Also
http://msdn2.microsoft.com/en-us/library/0zszyc6e.aspx


Preserving state is a lot of fun. It adds so much to the 'user
experience', where we all do it for. But persistence comes with
difficulties:


Simple types like int are converted to string when written to
'user.config', and cast back to (int) when read:

<setting name="Index" serializeAs="String">
<value>14</value>
</setting>


An object that contains several other objects, like MyMatrix, is not
automatically stored correctly in de user.log file:

<setting name="A_Matrix" serializeAs="Xml">
<value>
<Matrix xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
</value>
</setting>

and I want to have the floats contained in MyMatrix written to
'user.confing', of course.

Does this mean I need to write out the contents of MyMatrix by hand
(cumbersome and error-prone), delve into the intricacies of
System.ComponentModel.TypeConverter (huh?) or is there, hopefully, a
simple and direct way to serialize a class that contains simple types.
 

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

Back
Top