/// <summary> /// Saves settings using settings document. /// </summary> protected virtual void OnSave() { if (SettingsDocument != null) { SettingsFileOption fileOption = SettingsFileOption.Default; string extension = Path.GetExtension(FilePath); if (String.Equals(extension, ".stpe", StringComparison.InvariantCultureIgnoreCase)) { fileOption = SettingsFileOption.Encrypted; } else if (String.Equals(extension, ".stpc", StringComparison.InvariantCultureIgnoreCase)) { fileOption = SettingsFileOption.Compressed; } SettingsDocument.Save(FilePath, fileOption); } }
/// <summary> /// Saves this document to the specified file with the specified file options. /// </summary> /// <param name="filename">The filename.</param> /// <param name="fileOption">The file options.</param> public void Save(string filename, SettingsFileOption fileOption) { if (!File.Exists(filename)) { string directory = Path.GetDirectoryName(filename); if (!String.IsNullOrEmpty(directory)) { Directory.CreateDirectory(directory); } } //FileOptions.WriteThrough specifies that this file will be written directly to disk, and not to a disk cache. //SettingsDocuments can be used to save App configuration values, and this flag would ensure we've written to disk in an EMO situation using (FileStream fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite, _streamBufferSize, FileOptions.WriteThrough)) { Stream writable = null; ICryptoTransform encrypter = null; try { switch (fileOption) { case SettingsFileOption.Encrypted: Aes aes = AesManaged.Create(); aes.BlockSize = 128; aes.KeySize = 256; aes.IV = _encryptionIV; aes.Padding = PaddingMode.PKCS7; aes.Mode = CipherMode.CBC; aes.Key = _encryptionKey; encrypter = aes.CreateEncryptor(); writable = new CryptoStream(fileStream, encrypter, CryptoStreamMode.Write); break; case SettingsFileOption.Compressed: writable = new GZipStream(fileStream, CompressionMode.Compress); break; default: writable = fileStream; break; } using (TextWriter file = TextWriter.Synchronized(new StreamWriter(writable))) { WriteToFile(file); } } finally { if (writable != null) { writable.Dispose(); } if (encrypter != null) { encrypter.Dispose(); } } } }
/// <summary> /// Loads the specified settings file into this settings document by creating the settings node tree based on the file's /// tab structure. Any line that contains only whitespace or only a comment will not become a <see cref="SettingsNode"/> /// but will be retained when the file is saved. /// </summary> /// <param name="filename">The filename.</param> /// <param name="fileOption">The file option that was used when the file was saved.</param> /// <exception cref="FileNotFoundException">The file specified in <c>filename</c> was not found.</exception> public void Load(string filename, SettingsFileOption fileOption = SettingsFileOption.Default) { if (!File.Exists(filename)) { File.Create(filename).Dispose(); } using (FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read)) { if (fileOption == SettingsFileOption.Default) { string extension = Path.GetExtension(filename); if (String.Equals(extension, ".stpe", StringComparison.InvariantCultureIgnoreCase)) { fileOption = SettingsFileOption.Encrypted; } else if (String.Equals(extension, ".stpc", StringComparison.InvariantCultureIgnoreCase)) { fileOption = SettingsFileOption.Compressed; } } Stream readable = null; ICryptoTransform decrypter = null; try { switch (fileOption) { case SettingsFileOption.Encrypted: Aes aes = AesManaged.Create(); aes.BlockSize = 128; aes.KeySize = 256; aes.IV = _encryptionIV; aes.Padding = PaddingMode.PKCS7; aes.Mode = CipherMode.CBC; aes.Key = _encryptionKey; decrypter = aes.CreateDecryptor(); readable = new CryptoStream(file, decrypter, CryptoStreamMode.Read); break; case SettingsFileOption.Compressed: readable = new GZipStream(file, CompressionMode.Decompress); break; default: readable = file; break; } try { Logging.Log.Info("Settings", "Opening file '{0}'", filename); LoadFromStream(new StreamReader(readable)); Logging.Log.Info("Settings", "Loaded file '{0}'", filename); } catch (Exception ex) { if (readable != file) { Logging.Log.Error("Settings", ex); LoadFromStream(new StreamReader(file)); } else { throw; } } FileOption = fileOption; Name = filename; } finally { if (readable != null) { readable.Dispose(); } if (decrypter != null) { decrypter.Dispose(); } } } }