Using .NET 4, what is the best way to save an app's window size and position at closing and use these values to start the app's window next time it is run?
I prefer not to have to touch any kind of registry but don't know if there is some kind of app.config (similar to web.config for ASP.NET apps) that I can use for Windows Presentation Foundation apps.
Thanks.
Form_FormClosed
Form_Load
private void Form1_Load(object sender, EventArgs e)
{
this.Location = new Point(Properties.Settings.Default.LocationX, Properties.Settings.Default.LocationY);
this.Width = Properties.Settings.Default.WindowWidth;
this.Height = Properties.Settings.Default.WindowHeight;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.LocationX = this.Location.X;
Properties.Settings.Default.LocationY = this.Location.Y;
Properties.Settings.Default.WindowWidth = this.Width;
Properties.Settings.Default.WindowHeight = this.Height;
Properties.Settings.Default.Save();
}
MainWindow_Closed
MainWindow_Loaded
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
this.Left = Properties.Settings.Default.LocationX;
this.Top = Properties.Settings.Default.LocationY;
this.Width = Properties.Settings.Default.WindowWidth;
this.Height = Properties.Settings.Default.WindowHeight;
}
void MainWindow_Closed(object sender, EventArgs e)
{
Properties.Settings.Default.LocationX = this.Left;
Properties.Settings.Default.LocationY = this.Top;
Properties.Settings.Default.WindowWidth = this.Width;
Properties.Settings.Default.WindowHeight = this.Height;
Properties.Settings.Default.Save();
}
If you're going to save just one window position
and size
, I would suggest to save them in applicationSettings.
If you more window settings to save, or more windows to manage I would, personally suggest to save it in separate XML
file.
EDIT
Working with XML standart way example
LINQ to XML example
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With