Exemplo n.º 1
0
        /// <summary>
        /// 读取XML文件,生成自定义的结构体
        /// </summary>
        /// <returns></returns>
        public static XmlDllCollections Deserialize()
        {
            XmlDllCollections result = null;

            using (Stream fileStream = new FileStream(m_path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                XmlSerializer deserializer = new XmlSerializer(typeof(XmlDllCollections));
                result = (XmlDllCollections)deserializer.Deserialize(fileStream);
            }
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 读取Xml配置文件
        /// </summary>
        public void ReadFromXml()
        {
            //  序列化配置文件
            XmlDllCollections result = null;

            //  如果配置文件目录中不包含DllConf.xml,创建一个空的配置文件
            if (!File.Exists(m_path))
            {
                var emptyNode = new XmlDllCollections();
                Serialize(emptyNode);
            }
            //  读取配置文件
            using (Stream fileStream = new FileStream(m_path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                XmlSerializer deserializer = new XmlSerializer(typeof(XmlDllCollections));
                result = (XmlDllCollections)deserializer.Deserialize(fileStream);
            }

            if (result == null)
            {
                return;
            }
            //  清空配置文件中信息
            this.m_dllInfo.Infos.Clear();
            var temp = new XmlDllCollections();

            foreach (var item in result.Infos)
            {
                //  仅加载系统启用的dll
                if (item.Enabled)
                {
                    temp.Infos.Add(item);
                }
            }
            this.m_dllInfo     = temp;
            this.m_dllInfoCopy = temp;
        }
Exemplo n.º 3
0
        //public static void Query(out string assemblyString, out string classname, string tag)
        //{
        //    assemblyString = string.Empty;
        //    classname = string.Empty;

        //    var dlls = Deserialize();
        //    foreach (var item in dlls.Infos)
        //    {
        //        if (!item.Enabled)
        //            continue;
        //        foreach (var member in item.Members)
        //        {
        //            if (member.Tag.Equals(tag))
        //            {
        //                if (item.BaseDir.EndsWith(@"\"))
        //                    assemblyString = item.BaseDir + item.FileName;
        //                else
        //                    assemblyString = item.BaseDir + @"\" + item.FileName;

        //                classname = member.ClassName;
        //                break;
        //            }
        //        }
        //    }
        //}

        /// <summary>
        /// 序列化配置好的实例,XmlDllCollections是配置好路径的实例
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static bool Serialize(XmlDllCollections obj)
        {
            try
            {
                if (!Directory.Exists("Config"))
                {
                    // 创建文件夹
                    Directory.CreateDirectory("Config");
                }
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = new UTF8Encoding(false);
                settings.Indent   = true;
                XmlWriter     xw         = XmlWriter.Create(m_path, settings);
                XmlSerializer serializer = new XmlSerializer(typeof(XmlDllCollections));
                serializer.Serialize(xw, obj);
                xw.Close();
                return(true);
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine("程序集 XML 配置写入错误" + ex.Message);
                return(false);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 写入Xml配置文件
        /// </summary>
        public void WriteToXml()
        {
            try
            {
                if (this.m_dllInfo == null)
                {
                    return;
                }

                var temp = new XmlDllCollections();
                foreach (var item in this.m_dllInfo.Infos)
                {
                    if (item.Enabled)
                    {
                        temp.Infos.Add(item);
                    }
                }

                if (!Directory.Exists("Config"))
                {
                    // 创建文件夹
                    Directory.CreateDirectory("Config");
                }
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Encoding = new UTF8Encoding(false);
                settings.Indent   = true;
                XmlWriter     xw         = XmlWriter.Create(m_path, settings);
                XmlSerializer serializer = new XmlSerializer(typeof(XmlDllCollections));
                serializer.Serialize(xw, temp);
                xw.Close();
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine("程序集 XML 配置写入错误" + ex.Message);
            }
        }