0

When the user closes an application, I'd like to save some of the properties of the main application screen such as left and top coordinates, width, length, maximized, minimized, and perhaps some other state information. And then these will be used on the next startup to draw and position the main app screen etc.

What is the best way to do this? Where should the data be stored and in what format?

Thanks.

Randy Minder
  • 47,200
  • 49
  • 204
  • 358
  • already answers at https://stackoverflow.com/questions/453161/how-can-i-save-application-settings-in-a-windows-forms-application – divyang4481 May 30 '21 at 06:13
  • Does this answer your question? [How can I save application settings in a Windows Forms application?](https://stackoverflow.com/questions/453161/how-can-i-save-application-settings-in-a-windows-forms-application) – divyang4481 May 30 '21 at 06:16

3 Answers3

2

Easiest solution : bind the window / control properties you want to store to Settings properties.

You can get information about that here.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
Julien N
  • 3,880
  • 4
  • 28
  • 46
  • -1 for linking to an article that requires registration, is there another link? – Adam Houldsworth Jul 29 '10 at 14:42
  • I use the Settings for application data as well. It allows for User settings as well as Application settings. msdn here http://msdn.microsoft.com/en-us/library/aa730869%28VS.80%29.aspx – gooch Jul 29 '10 at 14:43
  • Swapped the link out for you. – Adam Houldsworth Jul 29 '10 at 14:45
  • @Adam I don't know how you access the link, but it doesn't requires registration... It may ask for it if you want, but you don't have to be registered to read it. – Julien N Jul 29 '10 at 14:46
  • @Julien when I clicked the original link it sent me to a page with an excerpt at the top - I've linked the actual article and removed my -1. Didn't realise you didn't need to register, the "Click to read this article" button was nested in a registration control - UI trickery me thinks lol – Adam Houldsworth Jul 29 '10 at 14:57
2

The simplest approach is to use Properties.Settings.

Links:

Using settings in WPF (or how to store/retrieve window pos and loc) Saving window size and location in WPF and WinForms (uses some P/Invoke)

But if your want to store data for many different windows Matthew MacDonald suggest you should create a helper class that stores a position for any window you pass in, using a registry key that incorporates the name of that window.

public class WindowPositionHelper 
{ 
    public static string RegPath = "Software\\MyApp\\WindowBounds\\"; 
 
    public static void SaveSize(Window win) 
    { 
        // Create or retrieve a reference to a key where the settings 
        // will be stored. 
        RegistryKey key; 
        key = Registry.CurrentUser.CreateSubKey(RegPath + win.Name); 
 
        key.SetValue("Bounds", win.RestoreBounds.ToString()); 
        key.SetValue("Bounds", 
          win.RestoreBounds.ToString(CultureInfo.InvariantCulture)); 
    }
public static void SetSize(Window win) 
    { 
        RegistryKey key; 
        key = Registry.CurrentUser.OpenSubKey(RegPath + win.Name); 
 
        if (key != null) 
        { 
            Rect bounds = Rect.Parse(key.GetValue("Bounds").ToString()); 
            win.Top = bounds.Top; 
            win.Left = bounds.Left; 
 
            // Restore the size only for a manually sized 
            // window. 
            if (win.SizeToContent == SizeToContent.Manual) 
            { 
                win.Width = bounds.Width; 
                win.Height = bounds.Height; 
            } 
        } 
    } 
}

The other approach is as Adam said in creating your custom serializable class, that will contain all the properties you need to store and manipulate with them accross all the life cycle of your app.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Eugene Cheverda
  • 8,760
  • 2
  • 33
  • 18
0

Might not be the best, but define a new class that contains these properties and XML serialize this to a file. When you load the application, look for the file and deserialize it if it exists.

Optionally, you could serialize this to user-specific isolated storage, so that different users' settings don't conflict.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187