Exemple #1
0
        public static bool Load()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(PropertiesRecord));

            serializer.UnknownNode      += (obj, eventArgs) => Critterworld.Log(new LogEntry("Unknown node " + eventArgs.Name + " in " + propertiesFileName));
            serializer.UnknownAttribute += (obj, eventArgs) => Critterworld.Log(new LogEntry("Unknown attribute " + eventArgs.Attr + " in " + propertiesFileName));
            try
            {
                using (FileStream fs = new FileStream(propertiesFileName, FileMode.Open))
                {
                    propertiesRecord = (PropertiesRecord)serializer.Deserialize(fs);
                    Critterworld.Log(new LogEntry("Properties file " + propertiesFileName + " loaded."));
                    return(true);
                }
            }
            catch (Exception e)
            {
                if (e is FileNotFoundException)
                {
                    Critterworld.Log(new LogEntry("Properties file " + propertiesFileName + " not found. Will be created."));
                }
                else
                {
                    Critterworld.Log(new LogEntry("Exception opening " + propertiesFileName + ": " + e.ToString()));
                }
                propertiesRecord = new PropertiesRecord();
                RestoreDefaults();
                return(false);
            }
        }
Exemple #2
0
        public static void Save()
        {
            if (propertiesRecord == null)
            {
                propertiesRecord = new PropertiesRecord();
                RestoreDefaults();
            }
            XmlSerializer serializer = new XmlSerializer(typeof(PropertiesRecord));

            try
            {
                using (FileStream fs = new FileStream(propertiesFileName, FileMode.Create))
                {
                    serializer.Serialize(fs, propertiesRecord);
                    Critterworld.Log(new LogEntry("Properties file " + propertiesFileName + " written."));
                }
            }
            catch (Exception e)
            {
                Critterworld.Log(new LogEntry("Exception writing " + propertiesFileName + ": " + e.ToString()));
                propertiesRecord = new PropertiesRecord();
                RestoreDefaults();
            }
        }