Ejemplo n.º 1
0
 /// <summary>
 /// 加载配置数据.如果加载失败,将抛异常
 /// </summary>
 public void LoadConfig()
 {
     SecurityOpr secOpr = new SecurityOpr(GlobalVar.Instanse.SecurityKey);
     string Datas = secOpr.ReadFromFile(m_StrPath);
     StringReader reader = new StringReader(Datas);
     using (m_Reader = new ResXResourceReader(reader))
     {
         try
         {
             foreach (DictionaryEntry item in m_Reader)
                 m_cfgDatas[item.Key.ToString()] = item.Value;
         }
         catch (FileNotFoundException)
         { }
         catch (Exception ex)
         {
             throw ex;
         }
     }
     //finally
     //{
     //    if (m_Reader != null)
     //        m_Reader.Close();
     //}
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Reads this instance.
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, ConfigData> Read()
        {
            Dictionary<string, ConfigData> datas = null;
            XmlReader reader = null;
            ConfigData data = null;
            SecurityOpr so = null;
            StringReader sr = null;
            try
            {
                datas = new Dictionary<string, ConfigData>();
                so = new SecurityOpr(m_Key);
                //从加密文件中读取出数据,并进行解密
                string buf = so.ReadFromFile(m_Path);
                if (buf.Equals(String.Empty))
                    return datas;
                //去掉XML文件结尾的一些无效的字符,因为在解密过程中,从内存取出的数据为原始字节,
                //因此字节串的长度应该等于2的指数,否则系统会自动在字节串结尾加空字节
                int pos = buf.LastIndexOf('>');
                if (pos != -1)
                    sr = new StringReader(buf.Substring(0, pos + 1));
                else
                    sr = new StringReader(buf);
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.CheckCharacters = false;
                settings.CloseInput = true;
                reader = XmlReader.Create(sr, settings);
                while (reader.Read())
                {
                    if (reader.Name.Equals("program") && reader.IsStartElement())
                    {
                        data = new ConfigData();
                        if (reader.HasAttributes)
                        {
                            while (reader.MoveToNextAttribute())
                            {
                                Type type = typeof(ConfigData);
                                FieldInfo finfo = type.GetField(reader.Name, BindingFlags.Instance | BindingFlags.NonPublic);

                                if (finfo != null)
                                {
                                    object value = Convert.ChangeType(reader.Value, finfo.FieldType);
                                    finfo.SetValue(data, value);
                                }
                            }
                        }
                    }
                    else if (reader.Name.Equals("path") && reader.IsStartElement())
                    {
                        reader.Read();
                        data.Path = reader.Value.Trim();
                        reader.Read();
                    }
                    else if (reader.Name.Equals("shortcut") && reader.IsStartElement())
                    {
                        reader.Read();
                        data.Shortcut = reader.Value.Trim();
                        reader.Read();
                    }
                    else if ((reader.Name.Equals("notice") || reader.Name.Equals("taskitem"))
                                && reader.IsStartElement())
                    {
                        ITrigerable item;
                        Type type;
                        string name = reader.Name;
                        if (name.Equals("taskitem"))
                        {
                            item = new TaskItem();
                            type = typeof(TaskItem);
                        }
                        else
                        {
                            item = new Notice();
                            type = typeof(Notice);
                        }

                        if (reader.HasAttributes)
                        {
                            while (reader.MoveToNextAttribute())
                            {

                                FieldInfo finfo = type.GetField(reader.Name, BindingFlags.Instance | BindingFlags.NonPublic);

                                if (finfo != null)
                                {
                                    object value = Convert.ChangeType(reader.Value, finfo.FieldType);
                                    finfo.SetValue(item, value);
                                }
                            }
                        }
                        //多一份拷贝,否则在修改运行任务的时候,扫描对象和修改对象为同一个,有可能会造成冲突
                        //虽然冲突没有什么大问题,现在的概率也较小,但这样不太好。
                        //if (name.Equals("taskitem"))
                        //    data.TaskItem = item.Clone() as TaskItem;

                        GlobalVar.Instanse.Trigers.Add(item);
                    }
                    else if (reader.Name.Equals("program") && !reader.IsStartElement())
                    {
                        datas.Add(data.Shortcut, data);
                    }
                }
            }
            catch (FileNotFoundException)
            {

            }
            catch (System.Xml.XmlException)
            {
                GlobalVar.Tip.Error("XML文档格式错误");
            }
            catch (Exception ex)
            {

                GlobalVar.Tip.Error(ex.Message);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }

            GlobalVar.Helper.AddInsert("programs", "shortcut,path,is_auto_run,title");
                DataTable dt = new DataTable("programs");
                GlobalVar.Helper.MakeSchemaFromObj(dt, typeof(ConfigData));

                foreach (KeyValuePair<string, ConfigData> pair in datas)
                {

                    ConfigData d = pair.Value;
                    GlobalVar.Helper.FillDataFromObj(dt, d);
                }
                GlobalVar.Helper.Update(dt);
            return datas;
        }