Пример #1
0
        public static CleanerConfig ParseConfig(string path)
        {
            //Try and read the config from the xml file

            //Get new config
            CleanerConfig config = new CleanerConfig();

            try
            {
                XDocument doc = XDocument.Load(path);
                foreach (var searchedDirectory in doc.Root.Element("paths").Elements("searchedDirectory"))
                {
                    string directoryPath = searchedDirectory.Element("path").Value;
                    int    time          = int.Parse(searchedDirectory.Element("deleteTime").Value);
                    config.SearchedDirectories.Add(new SearchedDirectory(directoryPath, time));
                }

                string strategy = doc.Root.Element("prompt").Attribute("type").Value;
                config.DeletionStrategy = strategy;
                config.PromptValue      = int.Parse(doc.Root.Element("prompt").Value);

                config.BreakTime    = int.Parse(doc.Root.Element("breakTime").Value);
                config.CombineTime  = int.Parse(doc.Root.Element("combineTime").Value);
                config.DelayedStart = int.Parse(doc.Root.Element("delayedStart").Value);
                config.DefaultKeep  = bool.Parse(doc.Root.Element("keepDefault").Value);
            }
            catch (Exception)
            {
                Console.WriteLine("Config file is invalid, dropping default config");
                config = DefaultConfig;
            }

            return(config);
        }
Пример #2
0
        /// <summary>
        /// Writes a config file as xml to the file
        /// </summary>
        /// <param name="config">the config to write</param>
        /// <param name="path">the path to the file</param>
        public static void SaveConfigToFile(CleanerConfig config, string path)
        {
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.NewLineChars    = "\r\n";
            settings.NewLineHandling = NewLineHandling.Replace;
            XmlWriter writer = XmlWriter.Create(path, settings);

            writer.WriteStartDocument();
            writer.WriteStartElement("settings");
            writer.WriteStartElement("paths");

            for (int i = 0; i < config.SearchedDirectories.Count; i++)
            {
                writer.WriteStartElement("searchedDirectory");
                WriteOneLineXmlElement(writer, "path", config.SearchedDirectories[i].Path);
                WriteOneLineXmlElement(writer, "deleteTime", config.SearchedDirectories[i].FileAgeLimit.ToString());
                writer.WriteEndElement();
            }

            writer.WriteEndElement();

            writer.WriteStartElement("prompt");
            writer.WriteAttributeString("type", config.DeletionStrategy);
            writer.WriteString(config.PromptValue.ToString());
            writer.WriteEndElement();

            WriteOneLineXmlElement(writer, "breakTime", config.BreakTime.ToString());
            WriteOneLineXmlElement(writer, "combineTime", config.CombineTime.ToString());
            WriteOneLineXmlElement(writer, "delayedStart", config.DelayedStart.ToString());
            WriteOneLineXmlElement(writer, "keepDefault", config.DefaultKeep.ToString());

            writer.WriteEndElement();
            writer.Close();
        }