public static Configuration Load()
 {
     if (_configuration == null)
     {
         FileStream fs = null;
         try
         {
             XmlSerializer xs = new XmlSerializer(typeof(Configuration));
             fs = new FileStream(path, FileMode.Open, FileAccess.Read);
             _configuration = (Configuration)xs.Deserialize(fs);
             fs.Close();
             return _configuration;
         }
         catch(Exception e)
         {
             if (fs != null)
                 fs.Close();
             throw e;
         }
     }
     else
     {
         return _configuration;
     }
 }
        public ConfigForm(Configuration config, MonitorNumChangedDelagate md, ReloadConfigDelagate rd)
        {
            InitializeComponent();

            showMonitor = md;
            reloadConfig = rd;

            _config = config;
            //填每页显示数
            comboBoxMonitorNum.SelectedIndex = _config.MonitorNum / 2;
            //填listView
            foreach (BerryDevice d in _config.BerryDevices)
                this.listViewDevices.Items.Add(d.IP).SubItems.Add(d.Name);
            //填路径
            textBoxPath.Text = _config.LogRootPath;

            comboBoxMonitorNum.SelectedIndexChanged += comboBoxMonitorNum_SelectedIndexChanged;
            listViewDevices.SelectedIndexChanged += listViewDevices_SelectedIndexChanged;
            propertyGridConfig.PropertyValueChanged += propertyGridConfig_PropertyValueChanged;
        }
        public static void Save(Configuration configuration)
        {
            if (configuration == null)
                throw new Exception("Parameter configuration is null!");

            FileStream fs = null;
            try
            {
                XmlSerializer xs = new XmlSerializer(typeof(Configuration));
                fs = new FileStream(path, FileMode.Create, FileAccess.Write);
                xs.Serialize(fs, configuration);
                _configuration = null;
                fs.Close();
            }
            catch(Exception e)
            {
                if (fs != null)
                    fs.Close();
                throw e;
            }
        }
 private void loadConfig()
 {
     try
     {
         _config = ConfigurationManager.Load();
     }
     catch (Exception e)
     {
         MessageBox.Show(e.ToString());
         _config = new Configuration();
         _config.AppPort = 7000;
         _config.LogRootPath = Directory.GetCurrentDirectory() + "\\" + "History";
         _config.MonitorNum = 4;
         _config.BerryDevices = new List<BerryDevice>();
         try
         {
             ConfigurationManager.Save(_config);
         }
         catch (Exception e2)
         {
             MessageBox.Show(e2.ToString());
         }
     }
 }