Example #1
0
        public void Initializes(string optionFile)
        {
            string optionFilePath = string.Empty;

            if (File.Exists(optionFile))
            {
                optionFilePath = optionFile;
            }
            else
            {
                optionFilePath = Path.Combine(ApplicationStartPath, optionFile);
            }

            this.OptionFile = OptionFile.Load(optionFilePath);

            this.OptionDocument = new XmlDocument();
            this.OptionDocument.Load(optionFilePath);

            StringCollection files = UtilityFile.SearchDirectory(ApplicationStartPath, "*.dll", true, true);

            foreach (string file in files)
            {
                Assembly ass   = Assembly.LoadFile(file);
                Type[]   types = ass.GetTypes();
                foreach (Type type in types)
                {
                    if (type.IsDefined(typeof(OptionAttribute), false))//如果Class被定制特性标记
                    {
                        object[] objs = type.GetCustomAttributes(false);
                        foreach (var obj in objs)
                        {
                            if (!(obj is OptionAttribute))
                            {
                                continue;
                            }
                            Option[] options = Option.Load(ass, type, (OptionAttribute)obj);
                            foreach (Option o in options)
                            {
                                o.OptionChangingEvent += new Option.OptionChangingEventHandler(OptionChangingEvent);
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        public static OptionFile Load(string optionFileFullPath)
        {
            OptionFile ofile = new OptionFile();

            try
            {
                if (!System.IO.File.Exists(optionFileFullPath))//如果用户的选项文件不存在,创建默认的选项文件
                {
                    using (XmlTextWriter w = new XmlTextWriter(optionFileFullPath, Encoding.UTF8))
                    {
                        w.Formatting = Formatting.Indented;
                        w.WriteStartDocument();
                        w.WriteStartElement("Options");
                        w.WriteAttributeString("ProductName", Application.ProductName);
                        w.WriteAttributeString("Created", DateTime.Now.ToString());
                        w.WriteAttributeString("ApplicationVersion", Application.ProductVersion);
                        w.WriteAttributeString("OptionVersion", "1");
                        w.WriteAttributeString("UpdateCount", "0");
                        w.WriteAttributeString("Modified", DateTime.Now.ToString());
                        w.WriteEndElement();
                        w.Flush();
                    }
                }
                ofile.File = new FileInfo(optionFileFullPath);
            }
            catch
            {
                //如果程序运行到这里,一般定义该选项文件虽然存在但发生了异常,将采取先删除再新建的办法处理
                try
                {
                    FileAttributes fileAtts = FileAttributes.Normal;
                    //先获取此文件的属性
                    fileAtts = System.IO.File.GetAttributes(optionFileFullPath);
                    //讲文件属性设置为普通(即没有只读和隐藏等)
                    System.IO.File.SetAttributes(optionFileFullPath, FileAttributes.Normal);
                    System.IO.File.Delete(optionFileFullPath);
                    Load(optionFileFullPath);
                }
                catch
                {
                    throw new IOException(string.Format("文件:{0}访问失败。\r\n原因:一般可能为被其他应用程序锁定。", optionFileFullPath));
                }
            }
            return(ofile);
        }
Example #3
0
 public static OptionFile Load(string optionFileFullPath)
 {
     OptionFile ofile = new OptionFile();
     try
     {
         if (!System.IO.File.Exists(optionFileFullPath))//如果用户的选项文件不存在,创建默认的选项文件
         {
             using (XmlTextWriter w = new XmlTextWriter(optionFileFullPath, Encoding.UTF8))
             {
                 w.Formatting = Formatting.Indented;
                 w.WriteStartDocument();
                 w.WriteStartElement("Options");
                 w.WriteAttributeString("ProductName", Application.ProductName);
                 w.WriteAttributeString("Created", DateTime.Now.ToString());
                 w.WriteAttributeString("ApplicationVersion", Application.ProductVersion);
                 w.WriteAttributeString("OptionVersion", "1");
                 w.WriteAttributeString("UpdateCount", "0");
                 w.WriteAttributeString("Modified", DateTime.Now.ToString());
                 w.WriteEndElement();
                 w.Flush();
             }
         }
         ofile.File = new FileInfo(optionFileFullPath);
     }
     catch
     {
         //如果程序运行到这里,一般定义该选项文件虽然存在但发生了异常,将采取先删除再新建的办法处理
         try
         {
             FileAttributes fileAtts = FileAttributes.Normal;
             //先获取此文件的属性
             fileAtts = System.IO.File.GetAttributes(optionFileFullPath);
             //讲文件属性设置为普通(即没有只读和隐藏等)
             System.IO.File.SetAttributes(optionFileFullPath, FileAttributes.Normal);
             System.IO.File.Delete(optionFileFullPath);
             Load(optionFileFullPath);
         }
         catch
         {
             throw new IOException(string.Format("文件:{0}访问失败。\r\n原因:一般可能为被其他应用程序锁定。", optionFileFullPath));
         }
     }
     return ofile;
 }