/// <summary> /// Reads a configuration using the specified encryption engine from the specified path /// </summary> /// <param name="encryptionEngine">The encryption engine to use while reading the configuration, null if no decryption is desired</param> /// <param name="configuration">The configuration to be read into</param> /// <param name="path">The path to be read from</param> /// <returns></returns> public static bool ReadConfiguration(FileEncryptionEngine encryptionEngine, out XmlConfiguration configuration, string path) { Stream stream = null; _lastException = null; try { configuration = new XmlConfiguration(); stream = (encryptionEngine != null ? encryptionEngine.CreateDecryptorStream(path) : new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None)); XmlConfigurationReader reader = new XmlConfigurationReader(); configuration = reader.Read(stream); configuration.Path = path; configuration.SetHasUnpersistedChanges(false); return(true); } catch (Exception ex) { Log.WriteLine(ex); _lastException = ex; } finally { if (stream != null) { stream.Close(); } } configuration = null; return(false); }
/// <summary> /// Writes a configuration using the specified encryption engine to the specified path /// </summary> /// <param name="encryptionEngine">The encryption engine to use while writing the configuration, null if no encryption is desired</param> /// <param name="configuration">The confiruration to write</param> /// <param name="path">The path to write it to</param> /// <returns></returns> public static bool WriteConfiguration(FileEncryptionEngine encryptionEngine, XmlConfiguration configuration, string path) { Stream stream = null; _lastException = null; try { if (configuration != null) { if (configuration.HasUnpersistedChanges()) { configuration.AcceptChanges(); stream = (encryptionEngine != null ? encryptionEngine.CreateEncryptorStream(path) : new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None)); XmlConfigurationWriter writer = new XmlConfigurationWriter(); writer.Write(configuration, stream, false); configuration.SetHasUnpersistedChanges(false); } return(true); } } catch (Exception ex) { Log.WriteLine(ex); _lastException = ex; } finally { if (stream != null) { stream.Close(); } } return(false); }
/// <summary> /// Reads a configuration using the specified encryption engine from the specified path /// </summary> /// <param name="encryptionEngine">The encryption engine to use while reading the configuration, null if no decryption is desired</param> /// <param name="configuration">The configuration to be read into</param> /// <param name="path">The path to be read from</param> /// <returns></returns> public static bool ReadConfiguration(FileEncryptionEngine encryptionEngine, out XmlConfiguration configuration, string path) { Stream stream = null; _lastException = null; try { configuration = new XmlConfiguration(); stream = (encryptionEngine != null ? encryptionEngine.CreateDecryptorStream(path) : new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None)); XmlConfigurationReader reader = new XmlConfigurationReader(); configuration = reader.Read(stream); configuration.Path = path; configuration.SetHasUnpersistedChanges(false); return true; } catch (Exception ex) { Log.WriteLine(ex); _lastException = ex; } finally { if (stream != null) stream.Close(); } configuration = null; return false; }
/// <summary> /// Writes a configuration using the specified encryption engine to the specified path /// </summary> /// <param name="encryptionEngine">The encryption engine to use while writing the configuration, null if no encryption is desired</param> /// <param name="configuration">The confiruration to write</param> /// <param name="path">The path to write it to</param> /// <returns></returns> public static bool WriteConfiguration(FileEncryptionEngine encryptionEngine, XmlConfiguration configuration, string path) { Stream stream = null; _lastException = null; try { if (configuration != null) { if (configuration.HasUnpersistedChanges()) { configuration.AcceptChanges(); stream = (encryptionEngine != null ? encryptionEngine.CreateEncryptorStream(path) : new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None)); XmlConfigurationWriter writer = new XmlConfigurationWriter(); writer.Write(configuration, stream, false); configuration.SetHasUnpersistedChanges(false); } return true; } } catch (Exception ex) { Log.WriteLine(ex); _lastException = ex; } finally { if (stream != null) stream.Close(); } return false; }