0

I'm trying to save my windows layout setting under an opened project.

In visual studio we do this by going to Tools-> Import and Export Setting and we can export the "Windows Layout" to a location. Basically, I need a C# code to do this. Is there a way in C# to get the current windows layout and save it in a project?

Nadeem_MK
  • 7,533
  • 7
  • 50
  • 61
  • Do you only want the window's position and size, or also those of controls on windows? Should a window be able to store and retrieve this by itself, or do you want to do it on the application level? Can windows be opened multiple times? [Have you tried](http://stackoverflow.com/questions/1873658/net-winforms-remember-windows-size-and-location) [searching](http://stackoverflow.com/questions/388859/get-list-of-open-windows-form-instance-that-are-excuted-from-different-assembly)? – CodeCaster Aug 25 '14 at 10:14
  • @CodeCaster: Ya basically the position, size and control on windows. I can be easily done in visual studio. They provide option for this but i want the same in my application which is a visual studio isolated shell application. So i need the c# code to do this – Satheesh Kurunthiah Aug 25 '14 at 10:20
  • windows cannot be opened multiple times. When this custom layout is saved under a project the next time i open the project i want the same custom windows layout – Satheesh Kurunthiah Aug 25 '14 at 10:22

1 Answers1

3

I have written a small class to save size/location and state of the Windows Form into XML and saves in FormSettings.xml file in MyApplication Folder (in your AppData). but yo can change the path where ever you want to store these settings. It uses serialization to read/write this data into XML

[Serializable]
public class FormSettings
{
    public FormWindowState WindowState { get; set; }
    public Size Size { get; set; }
    public Point Location { get; set; }
}

public static class FormExtensions
{
    private const String FolderName = "MyApplication";
    private const String PreferenceFileName = "FormSettings.xml";

    public static void LoadSettings(this Form form)
    {
        String myAppFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), FolderName);
        String formSettingFilePath = Path.Combine(assetCaptureFolderPath, PreferenceFileName);
        if (File.Exists(formSettingFilePath))
        {
            using (var sr = new StreamReader(formSettingFilePath))
            {
                XmlSerializer xmlser = new XmlSerializer(typeof(FormSettings));
                var formSettings = (FormSettings)xmlser.Deserialize(sr);
                if (formSettings != null)
                {
                    form.Size = formSettings.Size;
                    form.Location = formSettings.Location;
                    form.WindowState = formSettings.WindowState;
                }
            }
        }
    }

    public static void SaveSettings(this Form form)
    {
        FormSettings formSettings = new FormSettings();
        formSettings.WindowState = form.WindowState;
        if (form.WindowState == FormWindowState.Normal)
        {
            formSettings.Size = form.Size;
            formSettings.Location = form.Location;
        }
        else
        {
            formSettings.Size = form.RestoreBounds.Size;
            formSettings.Location = form.RestoreBounds.Location;
        }
        String myAppFolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), FolderName);
        String formSettingFilePath = Path.Combine(assetCaptureFolderPath, PreferenceFileName);
        using (var sw = new StreamWriter(formSettingFilePath))
        {
            XmlSerializer xmlSer = new XmlSerializer(typeof(FormSettings));
            xmlSer.Serialize(sw, formSettings);
        }
    }

Since these are extension methods, you only need to call like these from your Windows form

 this.SaveSettings(); //on close of your form & 
 this.LoadSettings (); // on your form load
Pankaj
  • 2,618
  • 3
  • 25
  • 47