all groups > dotnet academic > april 2004 >
You're in the

dotnet academic

group:

direction



direction Oren
4/27/2004 10:26:05 AM
dotnet academic: Hi all

I really need direction in this one

I have a WinApp in C# and I need to load data at the beginning and save data o
exit - just simple things like: this.Top, this.Left etc..
I have tried the resx - but it is READ ONLY
I have tried to use GetManifestResourceStream() from XML file (Embedded Resource
but the stream is for READ ONLY

Any suggestions ? Can you please add some code or linkz..

than
Re: direction Peter van der Goes
4/27/2004 1:19:41 PM

[quoted text, click to view]
Search for "serialization" in the help. You should find an article entitled
"Introducing XML Serialization".
As it appears from your question that you trying to persist objects ("this.
...."), the article will explain how it's done in the .NET Framework.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

Re: direction Angus Entwhistle
4/28/2004 4:53:01 PM
I do that all the time. It is dead easy.

What you want to do is to create a simple class that holds the stuff you
want persisted. At startup, deserialize that class and load the values from
its properties. At shutdown, store all the relevant values in the class'
properties and serialize it.

When you want to persist new stuff, simply add new properties to the class
and set them. The serializer will serialize the whole class without needing
any modifications for new items.

Following my signature is a simple settings class that stores form height,
width, left, and top, as well as a "ShowSplash" flag.

--
Angus

// In the main form...

// at the top
private Settings _settings;

private void SaveFormSettings()
{
_settings.Left = Left;
_settings.Top = Top;
_settings.FormHeight = Height;
_settings.FormWidth = Width;
}

private void LoadFormSettings()
{
if (_settings.Left > -1 && _settings.Top > -1
&& _settings.FormHeight > -1 && _settings.FormWidth > -1)
{
Top = _settings.Top;
Left = _settings.Left;
Width = _settings.FormWidth;
Height = _settings.FormHeight;
}
}

private void FormMain_Load(object sender, System.EventArgs e)
{
// Load any saved application settings.
_settings = new Settings();
LoadSettings(ref _settings);

this.Top = _settings.Top;
this.Left = _settings.Left;
this.Width = _settings,FormWidth;
this.Height = _settings.FormHeight;
}

private void FormMain_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{

_settings.Top = this.Top;
_settings.Left = this.Left;
_settings,FormWidth = this.Width;
_settings.FormHeight this.Height;

SaveSettings(_settings);
}

// Here is the magic...

#region LoadSettings
static public void LoadSettings(ref Settings settings)
{
string settingsfile = Path.Combine(Application.StartupPath,
"settings.xml");
if (File.Exists(settingsfile))
{
// Create an XML Serializer
XmlSerializer serializer = new XmlSerializer(typeof(Settings));

// Create an XmlTextReader using a FileStream.
Stream fs = new FileStream(settingsfile, FileMode.Open);
XmlTextReader reader = new XmlTextReader(fs);
try
{
// Deserialize using the XmlTextReader.
settings = (Settings)serializer.Deserialize(reader);
}
catch (Exception ex)
{
MessageBox.Show(this,
string.Format("Error Loading Application Settings:\n{0}", ex.Message),
"Application Settings Error!");
}

finally
{
reader.Close();
}
}
}
#endregion LoadSettings

#region SaveSettings
static public void SaveSettings(Settings settings)
{
string settingsfile = Path.Combine(Application.StartupPath,
"settings.xml");
// Serialize the settings class to an Xml file.
XmlSerializer serializer = new XmlSerializer(typeof(Settings));
// Create an XmlTextWriter using a FileStream.
Stream fs = new FileStream(settingsfile, FileMode.Create);
XmlTextWriter writer = new XmlTextWriter(fs, new
System.Text.UTF8Encoding());
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
try
{
// Serialize using the XmlTextWriter.
serializer.Serialize(writer, settings);
}
catch (Exception ex)
{
MessageBox.Show(this,
string.Format("Error Saving Application Settings:\n{0}", ex.Message),
"Application Settings Error!");
}
finally
{
writer.Close();
}
}
#endregion SaveSettings


Here is the settings class

#region Settings Class
/// <summary>
/// Manages all application settings and persists them from
/// one instance to another.
/// </summary>
[Serializable]
public class Settings
{
private int _top = -1;
private int _left = -1;
private int _formWidth = -1;
private int _formHeight = -1;
private bool _showSplash = true;

#region Constructor / Destructor and Clean-up
public Settings()
{
}
#endregion Constructor / Destructor and Clean-up

#region Application Settings

#region Top
public int Top
{
get { return _top; }
set {_top = value; }
}
#endregion Top

#region Left
public int Left
{
get { return _left; }
set {_left = value; }
}
#endregion Left

#region FormHeight
public int FormHeight
{
get { return _formHeight; }
set {_formHeight = value; }
}
#endregion FormHeight

#region FormWidth
public int FormWidth
{
get { return _formWidth; }
set {_formWidth = value; }
}
#endregion FormWidth

#region ShowSplash
public bool ShowSplash
{
get { return _showSplash; }
set { _showSplash = value; }
}
#endregion ShowSplash

}
#endregion Settings Class

AddThis Social Bookmark Button