public override bool Init()
        {
            ArchiveOption aOpt1 = new ArchiveOption("AOption1", new List <ArchPath> {
                new ArchPath("*.*", true, 10)
            });
            //aOpt1.IsSelected = true;
            ArchiveOption aOpt2 = new ArchiveOption("AOption2", new List <ArchPath> {
                new ArchPath("*.xml", true, 5), new ArchPath("*.config", true, 5)
            });

            aOpt2.IsSelected = true;
            ArchiveOption aOpt3 = new ArchiveOption("AOption3", new List <ArchPath> {
                new ArchPath("*.config", false, 5), new ArchPath("*.txt", false, 10)
            });

            ArchiveOption aOpt4 = new ArchiveOption("AOption4", new List <ArchPath> {
                new ArchPath("*.log", true, 10)
            });
            ArchiveOption aOpt5 = new ArchiveOption("AOption5", new List <ArchPath> {
                new ArchPath("*.exe", true, 5), new ArchPath("*.config", true, 5)
            });
            ArchiveOption aOpt6 = new ArchiveOption("AOption6", new List <ArchPath> {
                new ArchPath("*.dll", false, 5)
            });


            ArchiveOptions = new ArchiveConfigs {
                OptionList = new List <ArchiveOption> {
                    aOpt1, aOpt2, aOpt3, aOpt4, aOpt5, aOpt6
                }
            };

            return(IsInitialized = true);
        }
        public override bool Init()
        {
            try
            {
                Monitor.Enter(_ownLock);

                if (IsInitialized)
                {
                    Logger?.InfoLog("Already initialized.", CLASS_NAME);
                    return(IsInitialized);
                }

                if (!CheckFilePath())
                {
                    Logger?.InfoLog("The given xml file does not exists.", CLASS_NAME);

                    ArchiveOptions = new ArchiveConfigs(new List <ArchiveOption> {
                        new ArchiveOption("AllConfig", new List <ArchPath> {
                            new ArchPath("*.config", true, 10)
                        })
                    });

                    ArchiveOptions.Serialize(FilePath);

                    return(IsInitialized = true);
                }
                else
                {
                    if (!CheckFileAcessibility())
                    {
                        IsInitialized = false;
                        throw new Exception(Logger?.InfoLog("The given xml can not be opened.", CLASS_NAME));
                    }

                    lock (_fileLock)
                    {
                        ArchiveOptions = ArchiveConfigs.ReadFromFile(FilePath);
                    }
                }

                Logger?.InfoLog("Initialized.", CLASS_NAME);
                return(IsInitialized = true);
            }
            catch (Exception ex)
            {
                IsInitialized = false;
                string message = Logger?.ErrorLog($"Exception occured: {ex}", CLASS_NAME);
                throw new Exception(message, ex);
            }
            finally
            {
                Monitor.Exit(_ownLock);
            }
        }
示例#3
0
        public static bool WriteToFile(string path, ArchiveConfigs options)
        {
            try
            {
                XmlSerializer xmlser = new XmlSerializer(typeof(ArchiveConfigs));
                using (FileStream fs = new FileStream(path, FileMode.Create))
                {
                    xmlser.Serialize(fs, options);
                }

                return(true);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public override bool Save(ArchiveOption element)
        {
            lock (_ownLock)
            {
                if (!IsInitialized)
                {
                    throw new Exception(Logger?.InfoLog("Not initialized yet.", CLASS_NAME));
                }

                ArchiveOptions.OptionList.Add(element);

                lock (_fileLock)
                {
                    ArchiveConfigs.WriteToFile(FilePath, ArchiveOptions);
                }

                return(true);
            }
        }
示例#5
0
        public static ArchiveConfigs ReadFromFile(string path)
        {
            ArchiveConfigs serResult = null;

            try
            {
                if (!File.Exists(path))
                {
                    return(serResult);
                }

                XmlSerializer xmlser = new XmlSerializer(typeof(ArchiveConfigs));
                using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    serResult = (ArchiveConfigs)xmlser.Deserialize(fs);
                }

                return(serResult);
            }
            catch (Exception)
            {
                throw;
            }
        }