using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;

namespace XMLSetting
{
    public partial class Form1 : Form
    {
        // Create a global variable containing the name and
        // location of the settings file.
        String SettingsFile;

        public Form1()
        {
            InitializeComponent();

            // Store the location of the settings file.
            SettingsFile = Application.LocalUserAppDataPath + "\\Settings.XML";

            // Check for a settings file.
            if (File.Exists(SettingsFile))
            {
                // Load the file containing the settings.
                XDocument SettingData = XDocument.Load(SettingsFile);

                // Change the control settings to match the file settings.
                txtMessage.Text = SettingData.Root.Element("txtMessage").Value;
                chkChecked.Checked = Boolean.Parse(SettingData.Root.Element("chkChecked").Value);
            }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            // End the program.
            Close();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Create a document to hold the settings.
            XDocument SettingData = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XElement("Settings",
                    new XElement("txtMessage", txtMessage.Text),
                    new XElement("chkChecked", chkChecked.Checked)));

            // Save the settings to disk.
            SettingData.Save(SettingsFile);
        }
    }
}
