Beispiel #1
0
        public bool SaveToFile()
        {
            try
            {
                if (!Directory.Exists(SETTINGS_LOCATION))
                {
                    Directory.CreateDirectory(SETTINGS_LOCATION);
                }

                FileStream             writer     = new FileStream(SETTINGS_LOCATION + FILE_NAME, FileMode.Create, FileAccess.Write);
                DataContractSerializer serializer = new DataContractSerializer(_settingsModel.GetType(), KnownSettingTypes());
                serializer.WriteObject(writer, _settingsModel);
                writer.Close();
                return(true);
            }
            catch (Exception ex)
            {
                if (ex is DirectoryNotFoundException)
                {
                    Debug.WriteLine("ERROR: AppSettings save - 'settings' directory not found and could not be created!");
                }

                if (ex is FileNotFoundException)
                {
                    Debug.WriteLine("ERROR: AppSettings save - 'settings' file not found and / or could not be created!");
                }

                if (ex is SerializationException)
                {
                    Debug.WriteLine("ERROR: AppSettings save - serialization of settings file failed!");
                }

                if (ex is UnauthorizedAccessException)
                {
                    Debug.WriteLine("ERROR: AppSettings save - cannot access settings file - is it read only?");
                }

                return(false);
            }
        }