简体   繁体   中英

c# code to save the current windows layout under a project

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM