private void frmMain_Load(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;

            try
            {
                this.tvControllers.SuspendLayout();

                m_settings = UserSettings.LoadFromFile();
                m_settings.UserSettingsChanged += delegate(object obj, EventArgs ea)
                {
                    this.lblStatus.Text = m_settings.ToString();
                };
                this.lblStatus.Text = m_settings.ToString();

                m_controller = InfraredRayController.LoadIRCsFromFile();

                if (m_controller.Count > 0)
                {
                    foreach (string s in m_controller.Keys)
                    {
                        this.tvControllers.Nodes.Add(new TreeNode(s));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载配置文件和控制器信息失败,错误消息为:" + ex.Message);
            }
            finally
            {
                this.tvControllers.ResumeLayout();
                this.Cursor = Cursors.Default;
            }
        }
        private void tvControllers_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(e.Label))
            {
                e.CancelEdit = true;
                MessageBox.Show("控制器名称不能为空或者空格,请重新填写");
                e.Node.BeginEdit();
                return;
            }

            if (m_controller.ContainsKey(e.Label))
            {
                e.CancelEdit = true;
                MessageBox.Show(string.Format("控制器名称{0}已经存在,请重新填写", e.Node.Text));
                e.Node.BeginEdit();
                return;
            }

            InfraredRayController irc = new InfraredRayController();

            irc.Name = e.Label;
            m_controller.Add(e.Label, irc);
            this.tvControllers.SelectedNode = e.Node;
            e.Node.EndEdit(false);
            this.tvControllers.LabelEdit = false;
        }
        public static Dictionary <string, InfraredRayController> LoadIRCsFromFile()
        {
            System.IO.FileStream fs = null;
            try
            {
                string dirPath = Path.Combine(Application.StartupPath, DirName);

                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }

                string fileName = string.Format("*{0}", FileExtention);

                string[] files = Directory.GetFiles(dirPath, fileName);

                if (files == null || files.Length <= 0)
                {
                    return(new Dictionary <string, InfraredRayController>());
                }

                Dictionary <string, InfraredRayController> results = new Dictionary <string, InfraredRayController>();

                foreach (string s in files)
                {
                    fs = new System.IO.FileStream(s, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                    XmlSerializer         xs  = new XmlSerializer(typeof(InfraredRayController));
                    InfraredRayController irc = (InfraredRayController)xs.Deserialize(fs);
                    results.Add(irc.Name, irc);
                    fs.Close();
                }

                return(results);
            }
            catch (Exception ex)
            {
                throw new Exception("序列化到文件失败,错误消息为:" + ex.Message, ex);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }